如何将填充添加到NSMutableAttributedString?

时间:2018-09-14 16:38:19

标签: ios swift uilabel padding nsmutableattributedstring

我有一个标签,使用NSMutableAttributedString将文本写为:

older version

我想做的是降低星号的顶部填充,以使它位于MidY位置,甚至与如下所示的Cuisine一词一样:

enter image description here

如何使用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
}()

0 个答案:

没有答案
相关问题