Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift中为NSMutableAttributeString设置kCTUnderlineStyleAttributeName_Ios_Swift_Nsmutableattributedstring - Fatal编程技术网

Ios 在Swift中为NSMutableAttributeString设置kCTUnderlineStyleAttributeName

Ios 在Swift中为NSMutableAttributeString设置kCTUnderlineStyleAttributeName,ios,swift,nsmutableattributedstring,Ios,Swift,Nsmutableattributedstring,kCTUnderlineStyleAttributeName的文档包含以下内容: kCTUnderlineStyleAttributeName 要在渲染时应用于此属性的文本的下划线样式。值必须是CFNumber对象。默认值为kCTUnderlineStyleNone。设置kCTUnderlineStyleNone以外的值以绘制下划线。此外,CTUnderlineStyleModifiers中列出的常量可用于修改下划线的外观。下划线颜色由文本的前景色决定 setAttributes函数的签名如下:

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,非常感谢您抽出时间来解决这个问题。