title: Swift - NSMutableAttributedString changes all text in UITextView
body: <p><strong>Download an example I have created</strong></p>
  
  <p><a href="https://www.dropbox.com/s/1kialnssksvcxq3/MyTextViewToolbar.zip?dl=0" rel="nofollow noreferrer">Example Link</a></p>
  
  <p>I am adding a link to my UITextView like this: </p>
  
  <pre><code> 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
  </code></pre>
  
  <p>But after inserting it, it changes all the current text in the UITextView to the same font.</p>
  
  <p>So for example if I have some text that is Bold and some more text that is Italic, after I add the url(which is system font) it resets all the bold/italic text to the system font....</p>
  
  <p>If anyone knows how to keep the current font of the previous text I'd really appreciate the help.</p>
  
  <p>For any further explanation just drop a comment below!</p>
  
  <p>Many thanks in advance to anyone that can help!</p>
  
  <p><strong>Update</strong></p>
  
  <p>I am changing the text here in textDidChange </p>
  
  <pre><code>  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
   }
  </code></pre>
  
  <p>I have that so after adding the URL I make a space and reset the font so I don't continue typing as a URL link...Probably the problem but when I type normal and don't set a url link the text doesn't changing while make a space...</p>