gpt4 book ai didi

swift - 无法将类型 '[NSAttributedStringKey : Any]' 的值分配给类型 swift

转载 作者:行者123 更新时间:2023-11-28 09:39:22 26 4
gpt4 key购买 nike

我正在尝试在我的应用程序中创建一个超链接,并且正在实现此解决方案:

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

但它出错了:

Cannot assign value of type '[NSAttributedStringKey : Any]' to type '[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
]

swift 4.0

self.headerDescription.linkTextAttributes = [
NSAttributedStringKey.foregroundColor.rawValue : UIColor.blue,
NSAttributedStringKey.underlineStyle.rawValue : NSUnderlineStyle.styleSingle.rawValue
]

关于swift - 无法将类型 '[NSAttributedStringKey : Any]' 的值分配给类型 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53853347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com