如何在Swift中使用addAttribute

编程入门 行业动态 更新时间:2024-10-15 20:18:36
本文介绍了如何在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

本文发布于:2023-11-26 21:59:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Swift   addAttribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!