Невозможно присвоить значение типа '[NSAttributedStringKey: Any]' типу swift - PullRequest
0 голосов
/ 19 декабря 2018

Я пытаюсь создать гиперссылку в своем приложении и внедряю это решение:

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

Но при этом возникают ошибки:

Невозможно присвоить значениенаберите '[NSAttributedStringKey: Any]' для ввода '[String: Any]?' *

Что я делаю не так?

Вот мой полный код:

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

1 Ответ

0 голосов
/ 19 декабря 2018

Можно попробовать (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
]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...