swift - 无法分配类型“[NSAttributedStringKey:Any]”的值来快速键入
问题描述
我正在尝试在我的应用程序中创建超链接,并且正在实施此解决方案:
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,
]
}
}
解决方案
你可以试试(Swift 4.2)
self.headerDescription.linkTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.blue,
NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue
]
斯威夫特 4.0
self.headerDescription.linkTextAttributes = [
NSAttributedStringKey.foregroundColor.rawValue : UIColor.blue,
NSAttributedStringKey.underlineStyle.rawValue : NSUnderlineStyle.styleSingle.rawValue
]
推荐阅读
- kibana - 如何在 AWS Elasticsearch 的 Kibana 中配置 tilemap 服务器?
- python - 如何在python中将列表转换为json
- reactjs - UseState 和 useContext 在 React Typescript 中不能一起工作
- python - Json 显然被运行 Python 代码破坏了
- android - 如何使用滚动访问一个应用程序到另一个应用程序?
- css - Ant design 修复侧边菜单问题
- xamarin.forms - 链接 SDK 和用户程序集时 Xamarin Forms Google AdMob 错误
- javascript - 如何将 svg 水以角度填充动态
- sql - Hive Exists 条件不适用于多种条件
- mysql - 获取列总和并将其用于分配值