gpt4 book ai didi

swift - 使用 Swift iOS13 在 UITextView 中自定义嵌入式超链接

转载 作者:行者123 更新时间:2023-11-28 13:27:34 24 4
gpt4 key购买 nike

自安装 iOS13 以来,UITextView 扩展仅自定义文本周围的背景(白色),而不是整个 UITextView。

如何确保整个 UITextview 对象的背景颜色更改为白色?

enter image description here

扩展:

extension UITextView {


func hyperLink(originalText: String, hyperLink: String, urlString: String) {

let style = NSMutableParagraphStyle()
style.alignment = .center

let attributedOriginalText = NSMutableAttributedString(string: originalText)
let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
let fullRange = NSMakeRange(0, attributedOriginalText.length)
attributedOriginalText.addAttribute(NSAttributedString.Key.link, value: urlString, range: linkRange)
attributedOriginalText.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: fullRange)
attributedOriginalText.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black, range: fullRange)
attributedOriginalText.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.white, range: fullRange)
attributedOriginalText.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 11), range: fullRange)

self.linkTextAttributes = [
kCTForegroundColorAttributeName: UIColor.black,
kCTUnderlineStyleAttributeName: NSUnderlineStyle.single.rawValue,
] as [NSAttributedString.Key : Any]

self.attributedText = attributedOriginalText

}//end func
}

用法:

class LoginVC: UIViewController {

@IBOutlet weak var disclaimerTextView: UITextView!

override func viewDidLoad() {
super.viewDidLoad()

disclaimerTextView.hyperLink(originalText: "By continuing, you agree to our Terms and Privacy Policy found on our website", hyperLink: "website", urlString: WEBSITE_URL)

}
}

编辑:建议解决方案的输出 - 不起作用。

enter image description here

最佳答案

将扩展名更改为:

extension UITextView {
func hyperLink(originalText: String, hyperLink: String, urlString: String) {

let style = NSMutableParagraphStyle()
style.alignment = .center

let attributedOriginalText = NSMutableAttributedString(string: originalText)
let linkRange = attributedOriginalText.mutableString.range(of: hyperLink)
let fullRange = NSMakeRange(0, attributedOriginalText.length)
attributedOriginalText.addAttribute(NSAttributedString.Key.link, value: urlString, range: linkRange)
attributedOriginalText.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: fullRange)
attributedOriginalText.addAttribute(NSAttributedString.Key.font, value: UIFont.systemFont(ofSize: 11), range: fullRange)

self.linkTextAttributes = [
kCTForegroundColorAttributeName: UIColor.black,
kCTUnderlineStyleAttributeName: NSUnderlineStyle.single.rawValue,
] as [NSAttributedString.Key : Any]

self.attributedText = attributedOriginalText

}
}

正在为我工​​作。我只是删除了改变前景和背景颜色的属性文本属性。我不确定他们为什么会在那里。

关于swift - 使用 Swift iOS13 在 UITextView 中自定义嵌入式超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58108304/

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