下载我创建的示例
我正在添加一个链接到我的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中的所有当前文本更改为相同的字体。
因此,例如,如果我有一些Bold的文本和一些Italic的文本,在我添加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(_ :)委托方法。
UPDATE
我已从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
}
我从self.textView.font = systemFont
动作方法中删除了这行代码webLink(_ sender: AnyObject)
,这样它就不会改变textView的字体。
最后在textView(_:shouldChangeTextIn:replacementText:)
方法而不是检查用户是否已输入" "
字符串我正在检查用户是否输入了任何字符串并重新使用我在开头创建的字体属性,以便用户在输入后输入任何类型的文本链接它将是原始文本而不是为类似文本分配的文本。
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
}