[
我正在寻找一个pixelPerfect UI。我的设计师给了我一些特定字体的要求。我正在尝试实现它们,但是我遇到了一些有关style的问题。所有文本优先基准必须放置在网格上。将我的标题的maximumLineHeight设置为24(2x网格)时,一切都很好。但是,我的bodyText需要lineHeight为16。这会截断第一行的顶部。
我尝试设置标签高度,这只会增加布局冲突。我尝试将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")
}
}
这是我设置约束的类: 私人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),
])
}