SelectableTextView
A text view that supports selection and expansion.
The Problem
UILabel
and UITextView
offer unsatisfying support for text selection.
Existing solutions like TTTAttributedLabel are great but offer a somewhat limited API for text selection.
Installation
CocoaPods
Add the following to your Podfile
Carthage
Add the following to your Cartfile
Add to project Manually
Clone the repo and manually add the Files in SelectableTextView
Usage
Text Selection
To create selectable text, you have to create and register a validator. The validator must conform to the TextSelectionValidator
protocol.
You can unregister a validator at any time.
Custom Validators
Here is a resource for creating custom validators using the TextSelectionValidator
protocol.
There are other more specific protocols that make customization easier like ContainerTextSelectionValidator
and CompositeTextSelectionValidator
.
Prewritten Validators
There are a few prewritten validators supplied. These can be used as they are, as building blocks for other more complex validators, and as examples on how to build custom validators.
Text Validators
Abstract Validators
Link Validators
Customization is possible using the LinkValidatorAttributes
protocol. Example here.
Regex Validators
Text Expansion
You can add a text expansion button with the following method:
You can remove the expansion button using the following method:
Example:
You can customize the background color of the expansion button using the SelectedBackgroundColorAttribute
property HighlightedTextSelectionAttributes
struct as an attribute key.
Customization
text
- Sets the content of the text view
- Type:
String?
font
- Sets the font of the text view
- Type:
UIFont
- Defaults to
UIFont.systemFont(ofSize: 17)
textColor
- Sets the default text color
- Type:
UIColor
- Defaults to
UIColor.darkText
attributedText
- Overrides the
text
andtextColor
with the attributed text - Type:
NSAttributedString?
- Defaults to
nil
textAlignment
- Alignment of text in the text view
- Type:
TextAlignment
- Supports 3 types:
.left
,.right
,.center
- Defaults to
.left
lineBreakMode
- Determines how the text view handles new lines
- Type:
LineBreakMode
- Supports 1 type:
.wordWrap
-
- Defaults to
. wordWrap
- Defaults to
- See Goals
truncationMode
- Determines the bahavior of the last word in the last line of the text view
- Type:
TruncationMode
- Supports 2 types:
.clipping
,.truncateTail
- Defaults to
.clipping
- See Goals
numberOfLines
- Determines the number of lines in the text view
- Type:
Int
- Defaults to
0
- 0 lines means unbounded, similar to
UILabel
lineSpacing
- Determines the spacing between lines
- Type:
CGFloat
- Defaults to
0
- Supports negative values
textContainerInsets
- Sets the content inset of the text view
- Type:
UIEdgeInsets
- Defaults to
UIEdgeInsets.zero
selectionAttributes
- Sets the default selection attributes for selectable text
- Type:
[String : AnyObject]?
- Defaults:
color
=tintColor
,font
=boldSystemFont(ofSize: font.pointSize + 2)
isExpanded
- Tracks the state of the expansion button
- Type:
Bool?
- Defaults to
nil
. Will only return a value if the expansion button is added - If the expansion button is added, this property will toggle the state
textContentSize
- Readonly, returns the size of the text content
- Type:
CGSize
isSelectionEnabled
- Determines if selection is enabled for the text view
- Type:
Bool
- Defaults to
true
isScrollEnabled
- Determines if scrolling is enabled for the text view
- Type:
Bool
- Defaults to
false
scrollDelegate
- Forwards scrolling events fron the text view
- Type:
SelectableTextViewDelegate?
delegate
- Delegates work for the text view
- Type:
SelectableTextViewScrollDelegate?
Supported Escape Characters
- New Line
\n
- Tab
\t
- Null Terminator
\0
If you want to have text next to to a selectabe portion of text but still validate the text correctly, use the null terminator.
Miscelaneous
framesOfWordsMatchingValidator
You can get the relative frames of words within the text view with the method below. This is how I set up the stars effect in the first example gif.
Tab Length
You can adjust the number of spaces a tab character creates using TabTextModelConfig.numberOfSpaces
. The default value is 4.
Interface Builder
You can set most customization properties via interface builder. SelectableTextView
is marked as @IBDesignable
.
numberOfLines: Int
text: String
textColor: UIColor
lineSpacing: Float
isSelectionEnabled: Bool
isScrollEnabled: Bool
fontSize: Float
truncateTail: Bool
topTextInsets: Float
bottomTextInsets: Float
leftTextInsets: Float
rightTextInsets: Float
Delegate
Default implementations are provided for all SelectableTextViewDelegate
methods.
Scrolling
SelectableTextView
supports scrolling and forwards scroll events through SelectableTextViewScrollDelegate
.
You can also scroll to specific words or the first word that passes a validator.
Goals
- Character wrapping
- More truncation styles:
.head
,.center