highlight a text in textview
How can we highlight a text in textview like the imgage shown below,the user tap the select verse 开发者_运维问答it hilight the verse in yellow color and a popup window apper for that verse for doing action.How to do this? Thanks in advance.
It is only doable using CoreText, because it needs formatting attributes and NSAttributedString
to display text with multiple font style and colors.
You may be interested in my OHAttributedLabel
class that is a subclass of UILabel
to render NSAttributedString
(obviously it uses CoreText for this).
As setting the background color of a range of text is not directly possible/supported (no corresponding attribute in NSAttributedString
), you may need to draw the yellow rectangles (before drawing the text) yourself, but I've done this in my class too to manage highlighted links (see drawActiveLinkHighlightForRect:
method) so you may do a similar thing in your case.
精彩评论