UILabel文本具有不同的字体大小和颜色

lif*_*foo 10 uilabel ios swift

如何使用Swift在UILabel中设置不同的字体大小和颜色?

我需要为字符串的第一个字符着色,其颜色和大小与字符串的其余部分不同.

lif*_*foo 29

假设你想要一个更小的灰色货币符号,如下所示:

在此输入图像描述

只需使用一个NSMutableAttributedString对象:

let amountText = NSMutableAttributedString.init(string: "€ 60,00")

// set the custom font and color for the 0,1 range in string
amountText.setAttributes([NSFontAttributeName: UIFont.systemFontOfSize(12), 
                              NSForegroundColorAttributeName: UIColor.grayColor()],
                         range: NSMakeRange(0, 1))
// if you want, you can add more attributes for different ranges calling .setAttributes many times

// set the attributed string to the UILabel object
myUILabel.attributedText = amountText
Run Code Online (Sandbox Code Playgroud)

  • 这只是一个[自我回答](http://stackoverflow.com/help/self-answer) (11认同)