How to use addAttribute in Swift
Asked Answered
H

3

13

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_"]];

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_"))

I get the error

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?

Hyman answered 26/8, 2015 at 13:59 Comment(0)
T
34

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_"))
Timberwork answered 26/8, 2015 at 14:11 Comment(1)
The problem was I was using NSAttributedString instead of NSMutableAttributedStringBotanical
G
3

You are using Range rather than NSRange as required. Have a look at:

NSRange from Swift Range?

Gaberdine answered 26/8, 2015 at 14:6 Comment(0)
P
0
@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)
        
    }
}
Prostatectomy answered 25/3, 2022 at 7:5 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Penult

© 2022 - 2024 — McMap. All rights reserved.