ios - 在一个属性字符串中使用多种样式

标签 ios swift nsattributedstring

let myString = text1 + " (\(text2) people added)"
let myAttribute = [ NSAttributedStringKey.foregroundColor: UIColor.blue ]
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)
cell.textLabel?.attributedText = myAttrString

如何在一个属性字符串中使用不同样式的 text1"(\(text2) people added)"

最佳答案

是的,你可以

    var attributedText = NSMutableAttributedString()
    let str1 = text1
    let str2 = " (\(text2) people added)"
    let attr1 = [NSAttributedStringKey.foregroundColor: UIColor.blue]
    let attr2 = [NSAttributedStringKey.foregroundColor: UIColor.red]
    attributedText.append(NSAttributedString(string: str1, attributes: attr1))
    attributedText.append(NSAttributedString(string: str2, attributes: attr2))

    cell.textLabel?.attributedText = attributedText

关于ios - 在一个属性字符串中使用多种样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48458028/

相关文章:

objective-c - 点击 Storyboard 按钮时播放自定义声音

ios - 将 NSAttributed 文本附加到 UITextview

swift - 在 Swift 中仅颜色下划线

ios - UIMenuItem 上这个段的名称是什么?

ios - 使用通知从后台唤醒 iOS 应用程序

ios - 不支持推送导航 Controller - 自 Xcode 6.2 更新以来

swift - 如何使用Swift将句子的第一个字符大写

swift - 如何在完成处理程序中使用返回值?

ios - 在 Swift 中以编程方式更改 iAd 位置

ios - 将属性字符串附加到 UITextField 的属性文本并保留其所有以前的颜色和属性