首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift中使用addAttribute

如何在Swift中使用addAttribute
EN

Stack Overflow用户
提问于 2015-08-26 13:59:22
回答 3查看 22K关注 0票数 12

我试图添加到UITextViews的链接,所以我遵循这个职位中的代码。相关的目标-C代码是

代码语言:javascript
复制
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by @marcelofabri_"];
[attributedString addAttribute:NSLinkAttributeName
                         value:@"username://marcelofabri_"
                         range:[[attributedString string] rangeOfString:@"@marcelofabri_"]];

但当我在斯威夫特2中尝试这个

代码语言:javascript
复制
var attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: attributedString.string.rangeOfString("/marcelofabri_"))

我知道错误了

不能使用类型的参数列表调用“addAttribute”(字符串、值:字符串、范围:范围?)

我需要改变什么才能让它起作用?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-26 14:11:07

尝试使用NSString来查找范围而不是Swift String

代码语言:javascript
复制
var attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_")
attributedString.addAttribute(NSLinkAttributeName, value: "username://marcelofabri_", range: (attributedString.string as NSString).rangeOfString("/marcelofabri_"))
票数 34
EN

Stack Overflow用户

发布于 2015-08-26 14:06:55

您使用的是Range,而不是所需的NSRange。看一下:

来自斯威夫特范围的NSRange?

票数 3
EN

Stack Overflow用户

发布于 2022-03-25 07:05:38

代码语言:javascript
复制
@available(iOS 14, *)
extension ColorPickerVC: UIColorPickerViewControllerDelegate {
    
    func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
        
        print(viewController.selectedColor.hexCode)
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "setHexColor"), object: nil, userInfo: ["hexColor": viewController.selectedColor.hexCode])
    }
    
    func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "setHexColor"), object: nil, userInfo: ["hexColor": viewController.selectedColor.hexCode])
        self.view.backgroundColor = viewController.selectedColor
    }
    func canPerformSegueWithIdentifier(identifier: NSString) -> Bool {
        let templates:NSArray = value(forKey: "storyboardSegueTemplates") as! NSArray
        let predicate:NSPredicate = NSPredicate(format: "identifier=%@", identifier)
        
        let filteredtemplates = templates.filtered(using: predicate)
        return (filteredtemplates.count>0)
        
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32228765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档