2

我试图在输入 UITextView 时实现行间距。我有这个功能,当您在 UITextView 内单击时,它似乎只为直到光标位置的所有字符呈现行距。

当您在 UITextView 内单击时,如何编写函数以确保将格式应用于所有文本范围。

glyphRangeForTextContainer 是否有等效的方法来帮助我解决这个问题?

- (void) formatText
{
        __block CGFloat topOffset = 0;

        NSRange lineGlyphRange = [self.layoutManager glyphRangeForTextContainer:self.textContainer];

        [self.layoutManager 
         enumerateLineFragmentsForGlyphRange:lineGlyphRange usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *textContainer, NSRange glyphRange, BOOL *stop)
         {
             CGRect adjustedRect = rect;
             CGRect adjustedUsedRect = usedRect;
             adjustedRect.origin.y = topOffset;
             adjustedUsedRect.origin.y = topOffset;

             [self.layoutManager setLineFragmentRect:adjustedRect forGlyphRange:glyphRange usedRect:adjustedUsedRect];

             topOffset += 30; // 30 is the space between the lines you can adjust this as you like
         }];

        CGRect adjustedExtraLineFragmentRect = self.layoutManager.extraLineFragmentRect;
        CGRect adjustedExtraLineFragmentUsedRect = self.layoutManager.extraLineFragmentUsedRect;
        adjustedExtraLineFragmentRect.origin.y = topOffset;
        adjustedExtraLineFragmentUsedRect.origin.y = topOffset;

        [self.layoutManager setExtraLineFragmentRect:adjustedExtraLineFragmentRect usedRect:adjustedExtraLineFragmentUsedRect textContainer:self.textContainer];
}

问题与此线程有关

iOS7使用行间距输入UITextView并使用TextKit保持格式化

1- 这是问题的视频:- http://1drv.ms/1o8Rpd2

2-这是我正在测试的项目:http: //1drv.ms/1o8RtK0

谢谢你。

4

0 回答 0