Search code examples
iosswiftuitextviewrangefade

Fade the alpha value of range of text in UITextView


I have a UITextView which contain several concatenated NSAttributedStrings. I have a mechanism to detect which string or word that has been tapped, and would like to be able to fade the alpha value of the strings in and out.

Is it possible to manipulate only a range of the text in a TextView in such manner? As I understand, it appears only to be possible to change the alpha value of the entire TextView.


Solution

  • You may consider just throttling down the alpha of the NSForegroundColorAttributeName attribute value for that range of the string. If you are looking to fade the text entirely, bring the alpha down to zero and this will give you the exact effect you are looking for.

    [yourAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:255.0/255.0 green:0.0 blue:0.0 alpha:0.05]range:rangeOfAlphaText];