Ios 在Swift中为NSMutableAttributeString设置kCTUnderlineStyleAttributeName
kCTUnderlineStyleAttributeName的文档包含以下内容: kCTUnderlineStyleAttributeName 要在渲染时应用于此属性的文本的下划线样式。值必须是CFNumber对象。默认值为kCTUnderlineStyleNone。设置kCTUnderlineStyleNone以外的值以绘制下划线。此外,CTUnderlineStyleModifiers中列出的常量可用于修改下划线的外观。下划线颜色由文本的前景色决定 setAttributes函数的签名如下:Ios 在Swift中为NSMutableAttributeString设置kCTUnderlineStyleAttributeName,ios,swift,nsmutableattributedstring,Ios,Swift,Nsmutableattributedstring,kCTUnderlineStyleAttributeName的文档包含以下内容: kCTUnderlineStyleAttributeName 要在渲染时应用于此属性的文本的下划线样式。值必须是CFNumber对象。默认值为kCTUnderlineStyleNone。设置kCTUnderlineStyleNone以外的值以绘制下划线。此外,CTUnderlineStyleModifiers中列出的常量可用于修改下划线的外观。下划线颜色由文本的前景色决定 setAttributes函数的签名如下:
func setAttributes(attrs: [NSObject : AnyObject]?, range: NSRange)
我遇到的问题是,文档似乎暗示了这样一个事实,CTUnderlineStyle.Single可以(而且应该在Swift中)用作kCTUnderlineStyleAttributeName键的值。但是,前者是一个结构,因此不符合dictionaries值类型所需的AnyObject协议
有什么想法吗?我在这方面花了不少时间 该值需要面对
AnyObject
协议
不要使用cUnderlineStyle.Single
使用NSNumber
如下:
NSNumber(int: CTUnderlineStyle.Single.rawValue)
例如:
attributedString.addAttribute(kCTUnderlineStyleAttributeName, value:NSNumber(int: CTUnderlineStyle.Single.rawValue), range:NSRange(location:0,length:attributedString.length));
在swift 3中为我使用了此代码
let attributes:[AnyHashable : Any] = [kCTUnderlineStyleAttributeName as AnyHashable:NSNumber(value:CTUnderlineStyle.single.rawValue)]
明亮的感谢Vojtech,非常感谢您抽出时间来解决这个问题。