iOS7 Type into UITextView with Line Spacing and keep formatting using TextKit

十年热恋 提交于 2019-12-25 03:18:29

问题


I came across this great example which helped me understand how I can achieve line spacing / paragraph spacing as you type inside a UITextView with iOS7, however there is a problem and I am hoping someone can help me with resolving an issue as I am still learning about TextKit please.

This is the actual example https://github.com/downie/DoubleSpacedTextView

I have attached 2 files:-

1- This is a video of the problem:- http://1drv.ms/1o8Rpd2

2- This is the project I am testing with: http://1drv.ms/1o8RtK0

The issue is when there are blank lines between the text lines (at least 2 blank lines) and then click inside the blank lines to activate the UITextView and show the keyboard, the text moves up / loses its formatting.

This is the core function which I modified slightly from the original and it does the formatting, it works perfectly but not when you click inside the UITextView the first time:-

- (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];
}

Can you please see if we can stop this from happening? so that the text is always formatted as we type into the UITextView.

Are there any other examples that show how we can type a formatted text (with line spacing) into a UITextView using TextKit or attributed Text?.

I have been struggling with this problem since iOS6.

Thanks,

Will


回答1:


I don't have a full solution, just ideas of where to look for the solution.

All of the layout code fires off of the textChange event of the UITextView. Moving the cursor around doesn’t change any of the content of that text, so none of that code is called. However, even attaching it to textViewDidChangeSelection doesn’t make the issue go away.

I’m thinking it could be one of two things:

  • The NSLayoutManager is handling a blank line differently than a line with text. This either means it’s a TextKit bug, or it’s one of the many delegates/methods that just isn't being called.
  • Something about the rect math is wrong for a blank line. I believe the only key difference in layout if the line is blank vs not is that the usedRect for the lineFragmentRect has width 0, but maybe it’s also got height 0 or a bad offset that’s messing with the math. I don’t really see what’s causing that though.


来源:https://stackoverflow.com/questions/24129807/ios7-type-into-uitextview-with-line-spacing-and-keep-formatting-using-textkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!