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,
]
}
}