UITextView : link 属性的 foregroundColor 不改变(NSAttributedString 键)
UITextView : the foregroundColor of link attributes dont change (NSAttributedStringKey)
我对 NSAttributedStringKey 和 UITextView 有疑问
事实上,我正在尝试这样做:
这是理论上应该有效的代码
class ViewController: UIViewController,UITextViewDelegate {
@IBOutlet weak var txtView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
txtView.delegate = self;
let linkAttributes : [NSAttributedStringKey : Any] = [
.link: URL(string: "https://exemple.com")!,
.font:UIFont.boldSystemFont(ofSize: 18),
.foregroundColor: UIColor.blue] ;
let attributedString = NSMutableAttributedString(string: "Just
click here to do stuff...")
attributedString.setAttributes(linkAttributes, range:
NSMakeRange(5, 10))
txtView.attributedText = attributedString;
}}
但此代码显示此
所以我尝试通过添加两行额外的代码来解决问题:
let linkAttributes : [NSAttributedStringKey : Any] = [
.link: URL(string: "https://exemple.com")!,
.font:UIFont.boldSystemFont(ofSize: 18),
.foregroundColor: UIColor.blue,
.strokeColor : UIColor.black,//new line
.strokeWidth : -1,//new line
] ;
不幸的是,显示效果不是我想要的。
谁能帮我解决这个问题,我在网上搜索了一下,但一无所获。
提前致谢
INFO : the first image I took of this publication
:
我让你的代码只需稍作调整即可工作:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let basicAttributes : [NSAttributedStringKey : Any] = [
.font: UIFont.systemFont(ofSize: 18),
.foregroundColor: UIColor.black]
let linkAttributes : [NSAttributedStringKey : Any] = [
.link: URL(string: "https://www.whosebug.com")!,
.font:UIFont.boldSystemFont(ofSize: 18),
.foregroundColor: UIColor.blue]
let attributedString = NSMutableAttributedString(string: "Just click here to do stuff...", attributes: basicAttributes)
attributedString.addAttributes(linkAttributes, range: NSMakeRange(5, 10))
textView.attributedText = attributedString
}
您可能注意到我为整个字符串设置了字体(这可能不是必需的,特别是如果您已经在 XIB 或故事板中设置了 UITextView 对象的字体),但我猜真正的解决方案是使用addAttributes
,它将 link 属性添加到属性字符串中的其他属性。
似乎“.link”使用了 UITextView 的 tintColor。尝试设置 tintColor...
我对 NSAttributedStringKey 和 UITextView 有疑问
事实上,我正在尝试这样做:
这是理论上应该有效的代码
class ViewController: UIViewController,UITextViewDelegate {
@IBOutlet weak var txtView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
txtView.delegate = self;
let linkAttributes : [NSAttributedStringKey : Any] = [
.link: URL(string: "https://exemple.com")!,
.font:UIFont.boldSystemFont(ofSize: 18),
.foregroundColor: UIColor.blue] ;
let attributedString = NSMutableAttributedString(string: "Just
click here to do stuff...")
attributedString.setAttributes(linkAttributes, range:
NSMakeRange(5, 10))
txtView.attributedText = attributedString;
}}
但此代码显示此
所以我尝试通过添加两行额外的代码来解决问题:
let linkAttributes : [NSAttributedStringKey : Any] = [
.link: URL(string: "https://exemple.com")!,
.font:UIFont.boldSystemFont(ofSize: 18),
.foregroundColor: UIColor.blue,
.strokeColor : UIColor.black,//new line
.strokeWidth : -1,//new line
] ;
不幸的是,显示效果不是我想要的。
谁能帮我解决这个问题,我在网上搜索了一下,但一无所获。
提前致谢
INFO : the first image I took of this publication :
我让你的代码只需稍作调整即可工作:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let basicAttributes : [NSAttributedStringKey : Any] = [
.font: UIFont.systemFont(ofSize: 18),
.foregroundColor: UIColor.black]
let linkAttributes : [NSAttributedStringKey : Any] = [
.link: URL(string: "https://www.whosebug.com")!,
.font:UIFont.boldSystemFont(ofSize: 18),
.foregroundColor: UIColor.blue]
let attributedString = NSMutableAttributedString(string: "Just click here to do stuff...", attributes: basicAttributes)
attributedString.addAttributes(linkAttributes, range: NSMakeRange(5, 10))
textView.attributedText = attributedString
}
您可能注意到我为整个字符串设置了字体(这可能不是必需的,特别是如果您已经在 XIB 或故事板中设置了 UITextView 对象的字体),但我猜真正的解决方案是使用addAttributes
,它将 link 属性添加到属性字符串中的其他属性。
似乎“.link”使用了 UITextView 的 tintColor。尝试设置 tintColor...