下载我创建的示例
我正在像这样向我的 UITextView 添加一个链接:
let systemFont = self.text.font!
let linkAttributes = [
NSFontAttributeName : systemFont,
NSLinkAttributeName: NSURL(string: alertController.textFields![0].text!)!] as [String : Any]
let myAttributes2 = [ NSForegroundColorAttributeName: customGreen]
let attributedString = NSMutableAttributedString(string: "\(urlName)")
// Set the 'click here' substring to be the link
attributedString.setAttributes(linkAttributes, range: NSMakeRange(0, urlName.characters.count))//(0, urlName.characters.count))
self.text.linkTextAttributes = myAttributes2
self.text.textStorage.insert(attributedString, at: self.text.selectedRange.location)
let cursor = NSRange(location: self.text.selectedRange.location + "\(urlName)".characters.count, length: 0)
self.text.selectedRange = cursor
// self.text.font = systemFont
但是在插入之后,它会将 UITextView 中的所有当前文本更改为相同的字体。
例如,如果我有一些粗体文本和一些斜体文本,在我添加 url(这是系统字体)后,它会将所有粗体/斜体文本重置为系统字体....
如果有人知道如何保持以前文本的当前字体,我将非常感谢您的帮助。
如需任何进一步的解释,请在下方发表评论!
非常感谢任何可以提供帮助的人!
更新
我正在更改 textDidChange 中的文本
if text == " "
{
// let systemFont = self.text.font!
// let linkAttributes = [NSFontAttributeName : systemFont]
let attributes = [NSForegroundColorAttributeName: UIColor.black, NSFontAttributeName: self.text.font!] as [String : Any]
let attributedString = NSMutableAttributedString(string: text, attributes: attributes)
self.text.textStorage.insert(attributedString, at: range.location)
let cursor = NSRange(location: self.text.selectedRange.location+1, length: 0)
textView.selectedRange = cursor
return false
}
我有这个,所以在添加 URL 后我创建一个空格并重置字体,这样我就不会继续输入 URL 链接...可能是问题,但是当我正常输入并且不设置 url 链接时留出空格时文本不会改变...
最佳答案
let urlName = "google"
@IBAction func btnPressed(_ sender: Any) {
let systemFont = UIFont.systemFont(ofSize: 11)
let linkAttributes = [
NSFontAttributeName : systemFont,
NSLinkAttributeName: NSURL(string: "https://www.google.com")!] as [String : Any]
let myAttributes2 = [NSForegroundColorAttributeName: UIColor.green]
let attributedString = NSMutableAttributedString(string: "\(urlName)")
attributedString.setAttributes(linkAttributes, range: NSMakeRange(0, urlName.characters.count))//(0, urlName.characters.count))
self.text.linkTextAttributes = myAttributes2
self.text.textStorage.insert(attributedString, at: self.text.selectedRange.location)
let cursor = NSRange(location: self.text.selectedRange.location + "\(urlName)".characters.count, length: 0)
self.text.selectedRange = cursor
}
如果你想以编程方式添加文本,这就是你应该更新 textView 的方式。 在这种情况下不要使用 textViewDidChange(_:) 委托(delegate)方法。
更新
我已经从 Dropbox 下载了您的代码并对其进行了一些更改。
所以在这里我正在更改 viewDidLoad() 中的 textView 文本,例如。
我创建了两个常量以便在代码中再次使用它们,fontAttr 是具有名称和大小的原始字体,fontColorAttrBlue 是原始字体颜色。
let fontAttr = UIFont(name: "Helvetica-Bold", size: 20)
let fontColorAttrBlue = UIColor.blue
override func viewDidLoad() {
super.viewDidLoad()
let attrString = NSMutableAttributedString(string: "Hello World, How are you?", attributes: [NSFontAttributeName : fontAttr!, NSForegroundColorAttributeName: fontColorAttrBlue ])
self.textView.delegate = self // you can set the delegate from the storyboard.
self.textView.attributedText = attrString
}
而且我从 webLink(_ sender: AnyObject) 操作方法中删除了这行代码 self.textView.font = systemFont,这样它就不会更改 textView 的字体。
最后在 textView(_:shouldChangeTextIn:replacementText:) 方法中检查用户是否输入了 "" String 我检查用户是否输入了任何字符串并重用我在开始时创建的字体属性,这样如果用户在链接后输入任何类型的文本,它将是原始文本,而不是为类似文本分配的文本。
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text.characters.count > 0 {
let attributedString = NSMutableAttributedString(string: text, attributes: [NSFontAttributeName : self.fontAttr!, NSForegroundColorAttributeName: self.fontColorAttrBlue])
self.textView.textStorage.insert(attributedString, at: range.location)
let cursor = NSRange(location: self.textView.selectedRange.location+1, length: 0)
textView.selectedRange = cursor
return false
}
return true
}
关于ios - Swift - NSMutableAttributedString 更改 UITextView 中的所有文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40720922/