ios - 更改 NSMutableAttributedString 中单个字符的颜色

标签 ios objective-c xcode

我在 NSMutableString 中使用 ✔ 字符。我想将其颜色更改为绿色。我该如何改变它。我已经为它应用了颜色属性。

[String addAttribute:NSForegroundColorAttributeName
                       value:[UIColor greenColor]
                       range:NSMakeRange(107, 1)];

我已经用字符串中的其他正常字符尝试了这个解决方案。它改变了它的颜色。但特别是这个角色不会改变颜色。我怎样才能为这个角色使用绿色

最佳答案

var myString1:NSString = "(事件)"

    var myMutableString = NSMutableAttributedString()
    var str = NSString()
    str = "Name"
    myMutableString = NSMutableAttributedString(string: (str as String) + (myString1 as String), attributes: [NSFontAttributeName:UIFont(name: "Helvetica Neue", size: 18.0)!])

    myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.grayColor(), range: NSRange(location:str.length, length:myString1.length))


    myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Helvetica Neue", size: 14.0)!, range: NSRange(location: str.length,length: myString1.length))

关于ios - 更改 NSMutableAttributedString 中单个字符的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31459620/

相关文章:

ios - 如何通过索引访问特定行

ios - NSManagedObject 版本 : "objc_msgSend() selector name: _queueForDealloc" 崩溃

ios - 从 ios 库中的视频获取持续时间和创建日期

ios - 如何获取特定标签的 gmail 邮件,例如仅获取聊天邮件

objective-c - 开发一个在用户使用 xcode 登录之前运行的 osx 守护进程

ios - 当用户快速点击一个单元格时触发一个 Action

ios - 带整数的 switch 语句

ios - 容器 View Controller - 对开始/结束外观转换的不平衡调用

ios - 找不到 Cocoapod 框架

ios - #if 确定.h xcode 中的设备类型