BSText is a powerful text formatting library for iOS written in Swift. With BSText, you can easily create rich and dynamic text views, incorporating styles such as bold, italic, underline, strikethrough, and more. This documentation will guide you through the installation and usage of BSText, enabling you to enhance your text views effortlessly.
Getting Started
Installation
To integrate BSText into your iOS project, you can use CocoaPods or manually add the required files.
CocoaPods
1. Add the following line to your Podfile:
pod 'BSText'
2. Run the command:
pod install
3. Import BSText in your Swift file:
import BSText
Manual Installation
1. Download the latest BSText framework from the official repository.
2. Drag and drop the BSText framework into your Xcode project.
3. Import BSText in your Swift file:
import BSText
Usage
To start using BSText, follow these steps:
Step 1: Create an Instance of BSTextView
Create an instance of BSTextView and add it to your view hierarchy:
let textView = BSTextView()
view.addSubview(textView)
Step 2: Set the Text Content
Set the text content of the BSTextView using the attributed string property:
let attributedString = NSAttributedString(string: "Hello, BSText!")
textView.attributedText = attributedString
Step 3: Formatting Text Attributes
To format specific text within the BSTextView, use the BSTextAttribute class. Here’s an example of formatting a portion of the text as bold:
let attributedString = NSMutableAttributedString(string: "Hello, BSText!")
let textRange = NSRange(location: 7, length: 7) // Select the range to format
let boldFont = UIFont.boldSystemFont(ofSize: 16) // Define the desired bold font
attributedString.bs_set(font: boldFont, range: textRange) // Apply the bold font to the selected range
textView.attributedText = attributedString
Additional Features
Images
BSText allows you to easily insert images within your text. Here’s an example:
let attributedString = NSMutableAttributedString(string: "Hello, ")
let imageAttachment = BSTextAttachment()
imageAttachment.image = UIImage(named: "sampleImage")
imageAttachment.bounds = CGRect(x: 0, y: -4, width: 20, height: 20)
attributedString.bs_appendImage(with: imageAttachment)
textView.attributedText = attributedString
More Formatting Options
BSText provides a wide range of formatting options such as underline, strikethrough, shadow, background color, and more. The library also supports text layouts with line spacing, line break modes, and alignment. Make sure to explore the available methods and attributes to enhance your text views.
Conclusion
Congratulations! You are now ready to enhance your iOS text views using BSText. This guide demonstrated the installation process and provided an overview of basic usage and additional features. Discover the full potential of BSText and create stunning text layouts in your apps. Happy coding!