Rich Text Rendering Simplified with AttributedTextView Library

Creating an appealing and user-friendly text display is a crucial aspect of iOS application development. The open-source library, AttributedTextView, offers a streamlined approach to constructing rich text views within iOS applications. This library not only supports a wide array of text attributes such as fonts, font sizes, colors, background colors, hyperlinks, and images, but also facilitates custom text formatting. Moreover, it's updated to work seamlessly with the latest APIs of iOS 15.

Let's delve into some practical examples to showcase how AttributedTextView can be utilized:

Basic Usage

import UIKit
import AttributedTextView

class ViewController: UIViewController {
    @IBOutlet weak var textView: AttributedTextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Creating attributed text
        let attributedText = NSMutableAttributedString(string: "This is a simple attributed text view.")
        attributedText.addAttribute(.font, value: UIFont.systemFont(ofSize: 20), range: NSRange(location: 0, length: attributedText.length))
        attributedText.addAttribute(.foregroundColor, value: UIColor.black, range: NSRange(location: 0, length: attributedText.length))

        // Setting the attributed text
        textView.attributedText = attributedText
    }
}

Custom Rich Text

import UIKit
import AttributedTextView

class ViewController: UIViewController {
    @IBOutlet weak var textView: AttributedTextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Creating attributed text
        let attributedText = NSMutableAttributedString(string: "This is a simple attributed text view.")
        attributedText.addAttribute(.font, value: UIFont.systemFont(ofSize: 20), range: NSRange(location: 0, length: attributedText.length))
        attributedText.addAttribute(.foregroundColor, value: UIColor.black, range: NSRange(location: 0, length: attributedText.length))

        // Setting a hyperlink
        attributedText.addAttribute(.link, value: URL(string: "https://www.example.com/"), range: NSRange(location: 5, length: 10))

        // Inserting an image
        let image = UIImage(named: "image.png")
        attributedText.addAttribute(.image, value: image, range: NSRange(location: 20, length: 10))

        // Applying underline
        attributedText.addAttribute(.underlineStyle, value: NSUnderlineStyle.single, range: NSRange(location: 30, length: 10))

        // Setting background color
        attributedText.addAttribute(.backgroundColor, value: UIColor.red, range: NSRange(location: 40, length: 10))

        // Setting the attributed text
        textView.attributedText = attributedText
    }
}

Getting Started

  1. Install attributedtextview.
  2. Import attributedtextview into your iOS project.
  3. Utilize NSMutableAttributedString to create rich text.
  4. Employ the properties of attributedtextview to set the rich text.

Additional Information

  • AttributedTextView is developed using Swift.
  • It can be easily installed via CocoaPods.

In summary, AttributedTextView stands as an invaluable library for iOS developers, enabling them to swiftly craft and customize rich text views in their applications with ease.