【发布时间】:2016-09-20 04:44:08
【问题描述】:
我正在尝试做这样的事情:
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 加粗,如图所示。如果可能的话,我正在尝试将这段代码重写为我展示的第二种语法的形式。
谢谢!
【问题讨论】:
-
你想用属性文本做什么?
-
“难以弄清楚如何在包含的属性上包含范围”是什么意思?有什么问题?
-
我无法在第二种形式中包含范围。我一直在玩弄语法,但似乎无法包含它。我搜索了文档,但在第二种形式中我看不到任何包含范围等属性的东西,所以我不确定你是否可以这样做@NazmulHasan
-
你想在你的按钮中设置范围吗?
-
我想要的输出是
This is sample Text,This is sample是非粗体,Text是粗体 @UmairAfzal
标签: ios swift swift2 swift3 swift2.3