简体   繁体   English

Objective C - 核心文本和绘图线?

[英]Objective C - Core Text and drawing lines?

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. 因此,假设用户键入3个单词,然后选择正确的对齐按钮。 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. 单击按钮后,我创建了一个设置为rightAlignment的CTParagraphStyle,并设置了该范围的attributedString以包含此段落样式。

The problem is when the user is typing and CoreText starts drawing on the next line, the alignment goes back to left alignment automatically. 问题是当用户正在键入并且CoreText开始在下一行上绘制时,对齐会自动返回到左对齐。

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);
}

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. 基本上,您需要在当前光标位置之前查看属性,并将这些属性应用于新文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM