Friday, November 6, 2026
HomeiOS Developmentios - UITextView wealthy textual content enhancing

ios – UITextView wealthy textual content enhancing

[ad_1]

I have to implement a textual content editor utilizing UITextView that helps:

  • Daring/Italic/Underline
  • Shade,Font,font measurement modifications
  • Paragraph alignment
  • Listing format (bullets, numbers, and many others.)
  • Customized choice of textual content wherever within the textual content view and alter the properties

Up to now I’ve managed to do it with out NSTextStorage nevertheless it appears I’m hitting limits. As an illustration, to vary font, I exploit UIFontPickerViewController and alter the font as follows:

      func fontPickerViewControllerDidPickFont(_ viewController: UIFontPickerViewController) {
         if let selectedFontDesc = viewController.selectedFontDescriptor {
            let font = UIFont(descriptor: selectedFontDesc, measurement: selectedFontDesc.pointSize)
            self.selectedFont = font
        
            self.textView.typingAttributes = [NSAttributedString.Key.foregroundColor: self.selectedColor ?? UIColor.white, NSAttributedString.Key.font: self.selectedFont ?? UIFont.preferredFont(forTextStyle: .body, compatibleWith: nil)]
        
           if let vary = self.textView.selectedTextRange, let selectedFont = selectedFont {
             let attributedText = NSMutableAttributedString(attributedString: self.textView.attributedText)
            
             let location =  textView.offset(from: textView.beginningOfDocument, to: vary.begin)
             let size = textView.offset(from: vary.begin, to: vary.finish)
             let nsRange = NSRange(location: location, size: size)
            
             attributedText.setAttributes([NSAttributedString.Key.font : selectedFont], vary: nsRange)
            
             self.textView.attributedText = attributedText
         }
        
     }
 }

This works however the issue is it resets the colour of the chosen textual content and different properties. I would like to grasp a manner through which the present attributed of the textual content below choice are usually not disturbed. I think the best way to do is with utilizing NSTextStorage however I am unable to discover something good on web that explains the correct use of NSTextStorage to attain this.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments