2

I am trying to achieve linespacing when typing into a UITextView. I have this function which seems to render the linespacing only for all the characters upto the cursor position when you click inside the UITextView.

How do I write the function to ensure that the formatting is applied to all of the text range when you click inside the UITextView please.

is there an equivalent method perhaps for glyphRangeForTextContainer to help me with this?

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

The problem is related to this thread

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

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

Thank you.

Community
  • 1
  • 1
Wael
  • 489
  • 6
  • 19

0 Answers0