ios - 在 Swift 中为 NSMutableAttributedString 设置 kCTUnderlineStyleAttributeName

标签 ios swift nsmutableattributedstring

kCTUnderlineStyleAttributeName 的文档传达了以下内容:

kCTUnderlineStyleAttributeName The style of underlining, to be applied at render time, for the text to which this attribute applies. Value must be a CFNumber object. Default is kCTUnderlineStyleNone. Set a value of something other than kCTUnderlineStyleNone to draw an underline. In addition, the constants listed in CTUnderlineStyleModifiers can be used to modify the look of the underline. The underline color is determined by the text's foreground color.

setAttributes 函数的签名如下:

func setAttributes(attrs: [NSObject : AnyObject]?, range: NSRange)

我遇到的问题是文档似乎暗示了这样一个事实:CTUnderlineStyle.Single 可以(并且应该在 Swift 中)用作 kCTUnderlineStyleAttributeName 的值 键。但是,前者是一个结构体,因此不符合字典值类型要求的 AnyObject 协议(protocol)。

有什么想法吗?

最佳答案

我在这上面花了很多时间。

这个值需要面对AnyObject协议(protocol)。

而不是使用 CTUnderlineStyle.Single 使用 NSNumber 像这样:

NSNumber(int: CTUnderlineStyle.Single.rawValue)

例子:

attributedString.addAttribute(kCTUnderlineStyleAttributeName, value:NSNumber(int: CTUnderlineStyle.Single.rawValue), range:NSRange(location:0,length:attributedString.length));

关于ios - 在 Swift 中为 NSMutableAttributedString 设置 kCTUnderlineStyleAttributeName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25732805/

相关文章:

swift - 具有字体大小和 HTML 标记属性的 NSMutableAttributedString

iphone - 将 NSManagedObject 存储在字典中 (NSDictionary)

ios - URL session 初始化问题

iphone - .h 实现文件无法识别我的 UIViewController

ios - 从新的 Storyboard实例化 ViewController

ios - 线程 1 断点 1.1 错误 - swift

ios - NSFetchedResultsController 调用 didChangeObject 删除而不是更新

ios - adjustsFontSizeToFitWidth 在 iOS 14 上与 NSMutableAttributedString 无法正常工作

swift - 如何使用带属性字符串的系统字体?

ios - NSUserDefaults 中的数据仅在重新运行应用程序后刷新