Reputation: 223
I used CoreText to create the following text which fills a rectangle and surrounds an area in which I'll insert an image.
attrString = [[NSMutableAttributedString alloc] initWithString:string];
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTFontAttributeName, font);
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTForegroundColorAttributeName, ForegroundTextColor);
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef) attrString, CFRangeMake(0, attrString.length), kCTParagraphStyleAttributeName, paragraphStyle);
/* Get a framesetter to draw the actual text */
CTFramesetterRef fs = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef) attrString);
CTFrameRef frame = CTFramesetterCreateFrame(fs, CFRangeMake(0, attrString.length), pathToRenderIn, NULL);
/* Draw the text */
CTFrameDraw(frame, ctx);
/* Draw the text */
CTFrameDraw(frame, ctx);
In a later method, I'm using the attached statement to change the color of a subset of the text:
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(startRangeValue, endRangeValue)];
Unfortunately, the color doesn't change. Thanks in advance for your help.
Upvotes: 0
Views: 475
Reputation: 6211
I'm adding my comment as an answer since following it solved the problem:
After you add an attribute to a string you have to redraw. Basically what you see on the screen is not an NSString object that is changeable, it is a graphical drawing of an object that it read. If you change the object you have to tell it to draw the object again.
So the answer to the problem is that you need to call the drawing code every time you try to change anything about the string you are displaying.
Upvotes: 1