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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393

3 Answers3

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_"))
egor.zhdan
  • 4,555
  • 6
  • 39
  • 53
3

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

NSRange from Swift Range?

Community
  • 1
  • 1
Yarneo
  • 2,922
  • 22
  • 30
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)
        
    }
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 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](/help/how-to-answer). – Cristik Mar 25 '22 at 07:20