在Swift中为NSMutableAttributedString设置kCTUnderlineStyleAttributeName

Kyl*_*len 4 ios nsmutableattributedstring swift

kCTUnderlineStyleAttributeName的文档会转发以下内容:

kCTUnderlineStyleAttributeName 对于此属性适用的文本,在渲染时应用的下划线样式.值必须是CFNumber对象.默认值为kCTUnderlineStyleNone.设置除kCTUnderlineStyleNone之外的值的值以绘制下划线.此外,CTUnderlineStyleModifiers中列出的常量可用于修改下划线的外观.下划线颜色由文本的前景色决定.

setAttributes函数的签名如下:

func setAttributes(attrs: [NSObject : AnyObject]?, range: NSRange)
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是文档似乎暗示CTUnderlineStyle.Single可以(并且应该在Swift中)用作kCTUnderlineStyleAttributeName键的值.但是,前者是结构,因此不符合字典值类型所需的AnyObject协议.

有任何想法吗?

Voj*_*bka 6

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

价值需要面对AnyObject协议.

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

NSNumber(int: CTUnderlineStyle.Single.rawValue)
Run Code Online (Sandbox Code Playgroud)

例:

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