【发布时间】:2019-02-19 12:13:10
【问题描述】:
我有一个标签,它使用 NSMutableAttributedString 将文本写为:
我想要做的是降低星号的顶部填充,以便它的 midY 与下面的“美食”一词相同:
如何使用 NSMutableAttributedString 添加填充?
我知道我可以单独使用星号创建一个单独的标签,并使用带有常量的锚点将其居中,但我想看看如何使用 NSMutableAttributedString 实现这一点
let cuisineLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
let attributedText = NSMutableAttributedString(string: "Cuisine ", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17), NSAttributedStringKey.foregroundColor: UIColor.lightGray])
attributedText.append(NSAttributedString(string: "*", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 24), NSAttributedStringKey.foregroundColor: UIColor.red]))
label.attributedText = attributedText
return label
}()
【问题讨论】:
-
使用
baselineOffset属性。 developer.apple.com/documentation/foundation/nsattributedstring/… -
@Code 不同,谢谢它的工作
-
您可以使用许多其他星号字符之一,例如 ⁎、*、✱、✲、❋、* 等。
标签: ios swift uilabel padding nsmutableattributedstring