0

[破碎[1]

我正在寻找一个pixelPerfect UI。我的设计师对特定字体提出了一些要求。我正在尝试实现它们,但我遇到了段落样式的一些问题。所有文本第一基线必须位于网格上。将我的 Title 的 maximumLineHeight 设置为 24(2x 网格)时,一切都很好。但是,我的 bodyText 需要 16 的 lineHeight。这会切断第一行的顶部。

我试过设置标签高度,这只会增加布局冲突。我尝试将 firstBaseline 约束设置为远离其自身顶部的网格度量。无济于事。我还重写了 UILabel 的 draw 方法来添加一些额外的顶部填充。然而,无论我添加什么填充,这都会在我的代码中抛出 fristBaseline 约束使用。非常肮脏,只有最后不得已。

import Foundation

public class UILabelBody: UILabel  {


    override public var text: String? {
        didSet {
            let attributedString = NSMutableAttributedString(string: self.text!)
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.maximumLineHeight = 16

            attributedString.addAttributes([
                NSAttributedString.Key.kern: 0.4,
                NSAttributedString.Key.paragraphStyle : paragraphStyle],
                                           range: NSMakeRange(0, attributedString.length))
            self.attributedText = attributedString
        }
    }

    public init() {
        super.init(frame: CGRect.zero)
        self.translatesAutoresizingMaskIntoConstraints = false
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setup() {
        self.font = UIFont(name: "Oxygen", size: 16)
        self.textColor = UIColor(hexString: "1F1F1F")
    }
}

这是我设置约束的类: private func setup() {

    self.addSubview(contentBox)
    self.addSubview(imageView)
    self.addSubview(title)
    self.addSubview(body)


    imageView.backgroundColor = UIColor.yellow.withAlphaComponent(0.8)
    contentBox.backgroundColor = UIColor.red.withAlphaComponent(0.1)
    title.backgroundColor = UIColor.orange.withAlphaComponent(0.2)

    let padding = Layout.grid(1.5)

    let highPriority = UILayoutPriority(1000)
    let lowPrioririty = UILayoutPriority(100)

    let titleFirstBaseline = title.firstBaselineAnchor.constraint(equalTo: contentBox.topAnchor, constant: Layout.grid(2.5))
    let bodyFirstBaseLine =  body.firstBaselineAnchor.constraint(equalTo: title.lastBaselineAnchor, constant: Layout.grid(2))
    let selfHeight = self.heightAnchor.constraint(equalToConstant: Layout.grid(28))
    let selfWidth = self.widthAnchor.constraint(equalToConstant: Layout.grid(30))
    selfWidth.priority = highPriority
    selfHeight.priority = lowPrioririty

    titleFirstBaseline.priority = highPriority
    bodyFirstBaseLine.priority = highPriority


    NSLayoutConstraint.activate([


            selfWidth,
            selfHeight,

            contentBox.topAnchor.constraint(equalTo: self.topAnchor, constant: padding),
            contentBox.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: padding),
            contentBox.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -padding),
            contentBox.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -padding),

            imageView.leadingAnchor.constraint(equalTo: contentBox.leadingAnchor),
            imageView.topAnchor.constraint(equalTo: contentBox.topAnchor),
            imageView.widthAnchor.constraint(equalToConstant: Layout.grid(4)),
            imageView.heightAnchor.constraint(equalToConstant: Layout.grid(4)),

            title.leadingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: padding),
            titleFirstBaseline,
            title.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -padding),

            bodyFirstBaseLine,
            body.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: padding),
            body.bottomAnchor.constraint(equalTo: self.contentBox.bottomAnchor),
            body.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -padding),
    ])
}
4

1 回答 1

0

在酷刑室呆了几个小时后,我终于弄明白了!问题在于字体本身。它是 .otf 而不是 .ttf。因为这个 swift 不理解字体的内部 lineheight,所以把它剪掉了。切换到 .ttf 后问题已解决

在此处输入图像描述

于 2019-04-22T11:18:54.107 回答