Is it possible to change color of single word in UITextView and UITextField ?
If i have typed a word with a symbol infront (eg: @word) , can it's color be changed ?
Is it possible to change color of single word in UITextView and UITextField ?
If i have typed a word with a symbol infront (eg: @word) , can it's color be changed ?
Yes it is possible. However I have found it can be a headache trying to use
NSMutableAttributesString
with a SwiftRange
. The code below will get you around having to use theRange
class and return you an attributed string with the words highlighted a different color.Usage:
this is a swift implementation from @Anoop Vaidya answer,this function detect any word between {|myword|} , color these words in red and remove the special characters, hope this may help someone else:
you can use it like this:
Yes you need to use
NSAttributedString
for that, find the RunningAppHere.Scan through the word and find the range of your word and change its color.
EDIT:
EDIT 2 : see the screenshot
After setting an attributedtext you can set typingAttributes of
UITextView
with the values you want for you input field.Modified @fareed's answer for swift 2.0 and this is working (tested in a playground):