我正在尝试做这样的事情:
let introText = "This is sample "
let facebookText = "Text"
loginButton.setTitle("\(introText)\(facebookText)", forState: .Normal)
loginButton.addAttribute(NSFontAttributeName, value: UIFont(name: "Arial", size: 15.5)!, range: NSMakeRange(0, introText.characters.count))
loginButton.addAttribute(NSFontAttributeName, value: UIFont(name: "Arial-Bold", size: 15.5)!, range: NSMakeRange(introText.characters.count, facebookText.characters.count))
loginButton.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, introText.characters.count + facebookText.characters.count))
以这种形式:
loginButton.setAttributedTitle(NSMutableAttributedString(
string: "\(introText)\(facebookText)",
attributes: [
// attributes go here
NSForegroundColorAttributeName: UIColor.colorFromCode(0x151515),
]), forState: UIControlState.Normal)
我可以这样做吗?我无法弄清楚如何在包含的属性中包含范围。
编辑
抱歉,如果不是很明显,但是我的代码所做的是它创建了一个属性字符串,该字符串显示为“这是示例文本”,其中单词Text
加粗,如图所示。如果可能的话,我正在尝试将这段代码重写为我展示的第二种语法的形式。
谢谢!