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/