天天看点

ios开发text kit_iOS7使用行间距键入UITextView并使用TextKit保持格式化

ios开发text kit_iOS7使用行间距键入UITextView并使用TextKit保持格式化

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.

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

解决方案

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.