簡體   English   中英

Swift-NSMutableAttributedString更改UITextView中的所有文本

[英]Swift - NSMutableAttributedString changes all text in UITextView

下載我創建的示例

范例連結

我正在向我的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鏈接了……可能是問題所在,但是當我鍵入normal並且不設置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(_ :)委托方法。

更新

我已經從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 webLink(_ sender: AnyObject) ,以便它不會更改webLink(_ sender: AnyObject)的字體。

並且最后在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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM