iOS7 glyphRangeForTextContainer - 在UITextView中选择所有字形/所有文本范围

时间:2014-06-11 22:11:44

标签: ios7 uitextview textkit nslayoutmanager

我正在尝试在输入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 Type into UITextView with Line Spacing and keep formatting using TextKit

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

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

谢谢。

0 个答案:

没有答案