0

I'm trying to make a hyperlink in my app and I'm implementing this solution:

let attributedString = NSMutableAttributedString(string: "Just click here to register")
let url = URL(string: "https://www.apple.com")!

// Set the 'click here' substring to be the link
attributedString.setAttributes([.link: url], range: NSMakeRange(5, 10))

self.headerDescription.attributedText = attributedString
self.headerDescription.isUserInteractionEnabled = true
self.headerDescription.isEditable = false

// Set how links should appear: blue and underlined
self.headerDescription.linkTextAttributes = [
    NSForegroundColorAttributeName: UIColor.blue,
    NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue,
]

But it errors out on:

Cannot assign value of type '[NSAttributedStringKey : Any]' to type '[String : Any]?'

What am I doing wrong?

Here is my full code:

//
//  HeaderInfoTableViewCell.swift
//  Triage App
//
//  Created by Shay Vidas on 28/11/2018.
//  Copyright © 2018 Shay Vidas. All rights reserved.
//

import UIKit

class HeaderInfoTableViewCell: UITableViewCell {
    @IBOutlet weak var headerDescription: UITextView!

    override func awakeFromNib() {
        super.awakeFromNib()

        let attributedString = NSMutableAttributedString(string: "Just click here to register")
        let url = URL(string: "https://www.apple.com")!

        // Set the 'click here' substring to be the link
        attributedString.setAttributes([.link: url], range: NSMakeRange(5, 10))

        self.headerDescription.attributedText = attributedString
        self.headerDescription.isUserInteractionEnabled = true
        self.headerDescription.isEditable = false

        // Set how links should appear: blue and underlined
        self.headerDescription.linkTextAttributes = [
            NSForegroundColorAttributeName: UIColor.blue,
            NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue,
        ]
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Chief Madog
  • 1,738
  • 4
  • 28
  • 55
  • Depending on your Swift version, `linkTextAttributes` is either a dictionary which keys are `NSAttributedString.Key` or `String`. In your case, they are `String`, so you need to do: `myStringKey.rawValue`. But between the update you used the old one `NSForegroundColorAttributeName` (which is "Objective-C" look a like), so be coherent please. – Larme Dec 19 '18 at 14:34
  • Check this post: https://stackoverflow.com/questions/46314661/swift-4-conversion-error-nsattributedstringkey-any, you could use `headerDescription.linkTextAttributes = [ NSAttributedStringKey.foregroundColor.rawValue: UIColor.blue, NSAttributedStringKey.underlineStyle.rawValue: NSUnderlineStyle.styleSingle.rawValue, ] ` – LoVo Dec 19 '18 at 14:37

1 Answers1

6

You can try ( Swift 4.2 )

self.headerDescription.linkTextAttributes = [
    NSAttributedString.Key.foregroundColor: UIColor.blue,
    NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]

Swift 4.0

self.headerDescription.linkTextAttributes = [
        NSAttributedStringKey.foregroundColor.rawValue  : UIColor.blue,
        NSAttributedStringKey.underlineStyle.rawValue  : NSUnderlineStyle.styleSingle.rawValue
]
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87