About shtextviewblocks
shtextviewblocks is a powerful and easy-to-use library that enhances the functionality of UITextView in iOS applications. It provides various blocks-based methods that allow you to easily customize and manage text input, selection, and interaction with the UITextView.
Key Features
- Blocks-based text change observation
- Blocks-based text selection observation
- Customizable text formatting
- Easy text manipulation with built-in methods
- Support for placeholder text
- Seamless integration with existing UITextView instances
Installation
You can install shtextviewblocks via CocoaPods. Simply add the following line to your Podfile
:
pod 'shtextviewblocks'
Usage
Integration
To start using shtextviewblocks, follow these steps:
- Import the library into your project:
import shtextviewblocks
- Create an instance of UITextView (if you don’t already have one):
// Create a UITextView instance
let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
- Initialize the shtextviewblocks instance:
// Initialize the shtextviewblocks instance by calling the init method and passing your UITextView instance
let textViewBlocks = shtextviewblocks(textView: textView)
- Start customizing and managing your UITextView using the available blocks-based methods:
// Observe changes to text
textViewBlocks.onChange { newString in
// Handle text change
print("Text changed: \(newString)")
}
// Observe text selection changes
textViewBlocks.onSelectionChange { selectedRange in
// Handle selection change
print("Selection changed: \(selectedRange)")
}
Available Methods
onChange
Adds an observer block to be executed when the text in the UITextView changes.
Parameters:
block: (String) -> Void
– A block of code to be executed when the text changes. The block takes a single parameter, which is the new string value of the text.
Example:
textViewBlocks.onChange { newString in
// Handle text change
print("Text changed: \(newString)")
}
onSelectionChange
Adds an observer block to be executed when the text selection in the UITextView changes.
Parameters:
block: (NSRange) -> Void
– A block of code to be executed when the text selection changes. The block takes a single parameter, which is the new NSRange value representing the selected range.
Example:
textViewBlocks.onSelectionChange { selectedRange in
// Handle selection change
print("Selection changed: \(selectedRange)")
}
Text Formatting
shtextviewblocks provides easy-to-use methods for formatting the text within the UITextView.
Methods
setTextColor
Sets the color of the text within the UITextView.
Parameters:
color: UIColor
– The color to be applied to the text.
Example:
// Set text color to red
textViewBlocks.setTextColor(.red)
setFont
Sets the font of the text within the UITextView.
Parameters:
font: UIFont
– The font to be applied to the text.
Example:
// Set font to italic
textViewBlocks.setFont(UIFont.italicSystemFont(ofSize: 16))
Text Manipulation
shtextviewblocks provides built-in methods for manipulating the text within the UITextView.
Methods
insertText
Inserts the specified text at the current caret position within the UITextView.
Parameters:
text: String
– The text to be inserted.
Example:
// Insert "Hello, World!" at the current caret position
textViewBlocks.insertText("Hello, World!")
deleteBackward
Deletes the character immediately before the current caret position within the UITextView.
Example:
// Delete the character before the current caret position
textViewBlocks.deleteBackward()
Placeholder Text
shtextviewblocks supports the implementation of placeholder text within the UITextView.
Methods
setPlaceholder
Sets the placeholder text to be displayed within the UITextView when it is empty.
Parameters:
placeholder: String
– The placeholder text to be displayed.
Example:
// Set placeholder text
textViewBlocks.setPlaceholder("Enter your text here")
Conclusion
shtextviewblocks is an essential library for enhancing the functionality of UITextView in your iOS applications. By utilizing its blocks-based methods, you can easily customize and manage text input, selection, and interaction with ease. Start implementing it in your project today and experience the power of shtextviewblocks!