大佬教程收集整理的这篇文章主要介绍了ios – Swift – NSMutableAttributedString更改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中的所有当前文本更改为相同的字体.
因此,例如,如果我有一些粗体文本和一些更多的文本是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,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(_ :)委托方法.
我已从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 :)方法中,而不是检查用户是否已输入“”字符串我正在检查用户是否输入了任何字符串并重新使用我在开头创建的字体属性,以便用户在链接之后输入任何类型的文本它将是原始文本而不是为类似文本分配的文本.
func textView(_ textView: UITextView,shouldChangeTexTin range: NSRange,replacement@R_218_3801@: 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中的所有文本全部内容,希望文章能够帮你解决ios – Swift – NSMutableAttributedString更改UITextView中的所有文本所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。