HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – Swift – NSMutableAttributedString更改UITextView中的所有文本大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_675_0@
下载我创建的示例

Example Link

我正在添加一个链接到我的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(_ :)委托方法.

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
}

我从webLink(_ sender:AnyObject)动作方法删除了这行代码self.textView.font = systemFont,这样就不会改变textView的字体.

最后在textView(_:shouldChangeTexTinreplacementText :)方法中,而不是检查用户是否已输入“”字符串我正在检查用户是否输入了任何字符串并重新使用我在开头创建的字体属性,以便用户链接之后输入任何类型的文本它将是原始文本而不是为类似文本分配的文本.

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,请注明来意。