Reputation: 77616
I am using core text for a rich text editor. I allow users to change the text alignment.
So let's say the user types 3 words and then selects the right allignment button. After clicking the button I create a CTParagraphStyle with textAlignment set to right, and I set my attributedString for that range to contain this paragraph style.
The problem is when the user is typing and CoreText starts drawing on the next line, the alignment goes back to left alignment automatically.
What is the correct solution to extend this paragraph style to the rest of the lines in the current paragraph (As the user is typing, and core text redrawing)?
NSArray *lines = (NSArray*)CTFrameGetLines(_frame);
NSInteger count = [lines count];
CGPoint *origins = (CGPoint*)malloc(count * sizeof(CGPoint));
CTFrameGetLineOrigins(_frame, CFRangeMake(0, count), origins);
CGContextRef ctx = UIGraphicsGetCurrentContext();
for (int i = 0; i < count; i++) {
CTLineRef line = (CTLineRef)CFArrayGetValueAtIndex((CFArrayRef)lines, i);
CGContextSetTextPosition(ctx, frameRect.origin.x + origins[i].x, frameRect.origin.y + origins[i].y);
CTLineDraw(line, ctx);
}
Upvotes: 3
Views: 1038
Reputation: 53551
You need to keep track of the current "insertion attributes", that is, the attributes that should be applied to any text the user enters. Same goes for bold, italic, and so on. Basically, you'll need to look at the attributes before the current cursor position and apply those attributes to the new text.
Upvotes: 2