Reputation: 2198
I am using xcode 5, while using setting font to textview and i am drawing lines using CGContext
and i found some weird issue the value of font.lineHeight
changing between iOS6 and iOS7.
for example, in iOS6 font.lineHeight
= 25.000000 but
in iOS7 font.lineHeight
= 21.850000
So the lines are not drawing properly in textview.
This is my code, Note: self
is UITextview
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.font = [UIFont fontWithName:@"Helvetica" size:19];
}
return self;
}
- (void)drawRect:(CGRect)rect {
//Get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the line color and width
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.784 green:0.675 blue:0.576 alpha:1.0].CGColor);
CGContextSetLineWidth(context, 1.0f);
//Start a new Path
CGContextBeginPath(context);
//Find the number of lines in our textView + add a bit more height to draw lines in the empty part of the view
NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.lineHeight;
//Set the line offset from the baseline. (I'm sure there's a concrete way to calculate this.)
CGFloat baselineOffset = 3.5f;
//iterate over numberOfLines and draw each line
for (int x = 0; x < numberOfLines; x++) {
//0.5f offset lines up line with pixel boundary
CGContextMoveToPoint(context, self.bounds.origin.x, self.font.lineHeight*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, self.bounds.size.width, self.font.lineHeight*x + 0.5f + baselineOffset);
}
//Close our Path and Stroke (draw) it
CGContextClosePath(context);
CGContextStrokePath(context);
}
Upvotes: 1
Views: 2879
Reputation: 9915
Here is a screenshot of several lines of 19pt Helvetica in an iOS7 UILabel
. As you can see, there are 88px (non-retina) between two Ls spaced 4 lines apart. Since, 88 / 4 = 22, it would appear that 21.85 is indeed the correct line spacing.
Are you getting different results?
If the spacing changed from 25 to 22 between iOS6 and iOS7, I would guess Apple modified the font metrics in overhauling the look and feel of the OS.
Upvotes: 2