![](/img/trans.png)
[英]NSMutableAttributedString to UITextField in Swift
[英]Setting kCTUnderlineStyleAttributeName for an NSMutableAttributedString in Swift
kCTUnderlineStyleAttributeName的文档会转发以下内容:
kCTUnderlineStyleAttributeName对于此属性适用的文本,在渲染时应用的下划线样式。 值必须是CFNumber对象。 默认值为kCTUnderlineStyleNone。 设置除kCTUnderlineStyleNone之外的值的值以绘制下划线。 此外,CTUnderlineStyleModifiers中列出的常量可用于修改下划线的外观。 下划线颜色由文本的前景色决定。
setAttributes函数的签名如下:
func setAttributes(attrs: [NSObject : AnyObject]?, range: NSRange)
我遇到的问题是文档似乎暗示CTUnderlineStyle.Single可以(并且应该在Swift中)用作kCTUnderlineStyleAttributeName键的值。 但是,前者是结构,因此不符合字典值类型所需的AnyObject协议。
有任何想法吗?
我花了很多时间在这上面。
该值需要面对AnyObject
协议。
而不是使用CTUnderlineStyle.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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.