0

I have a subclass of a UITextView (Custom Control DALinedTextView) where i draw lined text. It works perfect on iOS5 and iOS6 but on iOS7 it fails (text does not match lines).

   - (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.0f);

    if (self.horizontalLineColor)
    {
        CGContextBeginPath(context);
        CGContextSetStrokeColorWithColor(context, self.horizontalLineColor.CGColor);

        // Create un-mutated floats outside of the for loop.
        // Reduces memory access.
        CGFloat baseOffset = 7.0f + self.font.descender;
        CGFloat screenScale = [UIScreen mainScreen].scale;
        CGFloat boundsX = self.bounds.origin.x;
        CGFloat boundsWidth = self.bounds.size.width;

        // Only draw lines that are visible on the screen.
        // (As opposed to throughout the entire view's contents)
        NSInteger firstVisibleLine = MAX(1, (self.contentOffset.y / self.font.lineHeight));
        NSInteger lastVisibleLine = ceilf((self.contentOffset.y + self.bounds.size.height) / self.font.lineHeight);
        for (NSInteger line = firstVisibleLine; line <= lastVisibleLine; ++line)
        {
            CGFloat linePointY = (baseOffset + (self.font.lineHeight * line));
            // Rounding the point to the nearest pixel.
            // Greatly reduces drawing time.
            CGFloat roundedLinePointY = roundf(linePointY * screenScale) / screenScale;
            CGContextMoveToPoint(context, boundsX, roundedLinePointY);
            CGContextAddLineToPoint(context, boundsWidth, roundedLinePointY);
        }
        CGContextClosePath(context);
        CGContextStrokePath(context);
    }

    if (self.verticalLineColor)
    {
        CGContextBeginPath(context);
        CGContextSetStrokeColorWithColor(context, self.verticalLineColor.CGColor);
        CGContextMoveToPoint(context, -1.0f, self.contentOffset.y);
        CGContextAddLineToPoint(context, -1.0f, self.contentOffset.y + self.bounds.size.height);
        CGContextClosePath(context);
        CGContextStrokePath(context);
    }
}

I know it's someting related to UIFont metrics..perphaps someone can help me out? I've change contentSize to intrinsicContentSize but it dos not work.

If i use systemFontOfSize it works perfectly, but with fontWithName it fails.

enter image description here

3 Answers3

0

Yes , i think you are using Custom Control DALinedTextView.

I am also facing a problem like you said in iOS7. Even you can't scroll that control correctly in iOS7.

First i used that control and now i leaved it and using built-in UITextView. :D

Perhaps , you need to change the font size and text margin or text padding of your textView.

Fire Fist
  • 7,032
  • 12
  • 63
  • 109
  • hey, but are you using lined uitextview? i've also done my own implementation and i cannot get it right, it only works with custom fonts (ttf) or with systemfont... –  Oct 01 '13 at 09:12
  • no bro. i am also using that DALinedTextView like you. it doesn't work in iOS7. – Fire Fist Oct 01 '13 at 13:36
  • yes bro. i know it . however i use custom font. That's why i can't use this control. :D – Fire Fist Oct 01 '13 at 17:09
  • if you find the solution, please let me know...otherwise i'll have to find .ttf files for all the fonts available on iOS.. –  Oct 01 '13 at 20:20
0

check this link. It uses NSLayoutManagerDelegate, that is used in ios7. For iOS 7, the styleString approach no longer works. You have to use NSLayoutManagerDelegate, it is easy to use.

txtViewNote.layoutManager.delegate = self;
txtViewNote.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"textView_bg_lines"]];

- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
return 20.5; // For really wide spacing
}
Community
  • 1
  • 1
iPhone Programmatically
  • 1,211
  • 2
  • 22
  • 54
0

I've been struggling with this weird issue for quite a while, but finally stumbled upon a legit solution:

textView.layoutManager.usesFontLeading = NO;

This makes UITextView render text (almost) identically to UILabel.

secondcitysaint
  • 1,158
  • 12
  • 20