Reputation: 115
I have a textView that contains an editable text (its color and font attributes are set by the user) I implemented an attributedText in order to achieve this, but when the textView is emptied (there's no text in it) the attributes are still the ones chosen before. i.e. if red color is chosen, when all text is deleted and if new text is typed it keeps the red color, I want this new text to have the default values of color and font. I have tried to set the attributedText to nil, but I don't know how to indicate that the textView has no text, I have the following code in a UITextView extension:
func setAttributedValueAtSelectedTextRange(_ attributeName: String, value: Any) {
let textRange = selectedRange
let selectedText = NSMutableAttributedString(attributedString: attributedText)
selectedText.addAttribute(attributeName, value: value, range: textRange)
attributedText = selectedText
selectedRange = textRange
attributedText = nil
}
Upvotes: 1
Views: 2406
Reputation: 90681
The attributes that will be applied to newly-typed text come from the typingAttributes
property. That gets changed automatically when text is selected, so the new text matches what is replaced or, if the selection is empty, what precedes it.
In your case, you can just set it to an empty dictionary to clear it.
Upvotes: 3
Reputation: 197
You need to reset the attributes back to default when you determine the right point. Ie. textField.textColor = UiColor.black
Upvotes: 1