ioscore-text

CoreText - change text color when tapped


I am using CoreText to draw text to my table view cells as I implemented the fast scrolling technique. Links in my text are a different color and respond to clicks, but I cannot figure out how to get the links to look like they are being clicked i.e. change color when tapped then change back. I have seen this in multiple apps, but not sure how it is done. Can this be done with CoreText or do I need to use something else?

I know this can be done with UILabels but that defeats the purpose of drawing text directly in the cell's content view.

Thanks.


Solution

  • Each time you want to change the color of the text, you need to send a -setNeedsDisplayInRect: to the view in which you are drawing the text. In addition, set a variable corresponding to the text color.

    When the view redraws and invokes your drawLayer:inContext:, make sure that kCTForegroundColorAttributeName is set to the correct color (based off of the variable you set) and redraw your text.

    You can also use a similar technique but with the methods in UIKit/UIStringDrawing.h rather than Core Text.