Reputation: 2173
I'm trying to make a RichTextEditor in my app.
The users can change both foreground color and background color when they're editing.
Here's how I set typingAttributes:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
self.setTypingColor(foregroundColor: self.foregroundColor, backgroundColor: self.backgroundColor)
return true
}
But I found that if the user inputs the \n
, the editor will also apply background color to the whole lines.
Here's what I want it to be.
That is, I want it to apply these colors only on the characters, not the \n
, \t
nor \r
.
Is there a way to do it without creating an AttributedString
by my self?
Upvotes: 0
Views: 202
Reputation: 1209
Something like this might work in the simple case.
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" { // better matching required
self.setTypingColor(foregroundColor: .clear, backgroundColor: .clear)
} else {
self.setTypingColor(foregroundColor: self.foregroundColor, backgroundColor: self.backgroundColor)
}
return true
}
But the problem will probably persist when the user pastes a string into your text view in which case I think you may have to start handling the attributed string.
Upvotes: 1