如何在Swift中使用addAttribute [英] How to use addAttribute in Swift
本文介绍了如何在Swift中使用addAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试添加到UITextViews的链接,所以我遵循此帖子.相关的Objective-C代码是
I'm trying to add links to UITextViews, so I am following the code in this post. The relevant Objective-C code is
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
value:@"username://marcelofabri_"
range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];
但是当我在Swift 2中尝试这样做
But when I try this in Swift 2 as
var attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: attributedString.string.rangeOfString("/marcelofabri_"))
我收到错误
不能使用类型为'(String,value:String,range:Range?)'的参数列表调用'addAttribute'
Cannot invoke 'addAttribute' with an argument list of type '(String, value: String, range: Range?)'
要使它正常工作,我需要更改什么?
What do I need to change to get this to work?
推荐答案
尝试使用 NSString
而不是Swift String
查找范围:
Try using NSString
to find range instead of Swift String
:
var attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: (attributedString.string as NSString).rangeOfString("/marcelofabri_"))
这篇关于如何在Swift中使用addAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文