12

I was planning on using NSAttributedString to highlight portions of strings with the matching query of a user's search. However, I can't find an iOS equivalent of NSBackgroundColorAttributeName—there's no kCTBackgroundColorAttributeName. Does such a thing exist, similar to the way NSForegroundColorAttributeName becomes kCTForegroundColorAttributeName?

kevboh
  • 5,207
  • 5
  • 38
  • 54
  • 1
    See if this helps http://stackoverflow.com/questions/5424003/changing-background-color-on-nsattributedstring – Oscar Del Ben Jul 27 '11 at 06:35
  • Thanks, unfortunately since NSAttributedString is laid out in CoreText I don't think there are views for which I can change the background color. – kevboh Jul 28 '11 at 15:31
  • @kevboh I have written a HighlightLabel (http://github.com/dineshrajas/HighlightLabel). Go ahead, If you wanna still need it. – Dinesh Raja Aug 04 '13 at 11:09

2 Answers2

8

No, such an attribute doesn't exist in Core Text, you'll have to draw your own rectangles underneath the text to simulate it.

Basically, you'll have to figure out which rectangle(s) to fill for a given range in the string. If you do your layout with a CTFramesetter that produces a CTFrame, you need to get its lines and their origins using CTFrameGetLines and CTFrameGetLineOrigins.

Then iterate over the lines and use CTLineGetStringRange to find out which lines are part of the range you want to highlight. To get the rectangles to fill, use CTLineGetTypographicBounds (for the height) and CTLineGetOffsetForStringIndex (for the horizontal offset and width).

omz
  • 53,243
  • 5
  • 129
  • 141
  • 2
    Great answer. It's a shame a simple attribute isn't around to replace all that code. – kevboh Aug 01 '11 at 15:25
  • 1
    My DTCoreText project supports highlighting the same way it works on Mac. You have an NSAttributedString initWithHTMLData method to get form HTML to attributed string and a view class that properly displays attributed strings with many additional features over CATextLayer. – Cocoanetics Dec 25 '12 at 09:46
4

NSBackgroundColorAttributeName is available in iOS 6, and you can use it by following way:

[_attributedText addAttribute: NSBackgroundColorAttributeName value:[UIColor yellowColor] range:textRange];

[_attributedText drawInRect:rect]; 

drawInRect: will support NSBackgroundColorAttributeName and all NS*AttributeNames supported by iOS 6.

For CTFrameDraw() there is no support for background text color.

Code:

- (void)drawRect:(CGRect)rect {    

    // First draw selection / marked text, then draw text

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    [_attributedText drawInRect:rect];

    CGContextRestoreGState(context);

//  CTFrameDraw(_frame, UIGraphicsGetCurrentContext());

}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
atrane
  • 398
  • 6
  • 20