The code is extracted from SimpleTextInput sampe code, with a bit modification.
Create the frame:
self.font = [UIFont systemFontOfSize:18.f];
CTFontRef ctFont = CTFontCreateWithName((CFStringRef) self.font.fontName, self.font.pointSize, NULL);
self.attributes = [[NSDictionary dictionaryWithObject:(id)ctFont forKey:(NSString *)kCTFontAttributeName] retain];
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.text attributes:self.attributes];
_framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
// Create the Core Text frame using our current view rect bounds
UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
_frame = CTFramesetterCreateFrame(_framesetter, CFRangeMake(0, 0), [path CGPath], NULL);
line below is any line of the frame (get by CTFrameGetLines(_frame)
):
CGFloat ascent, descent, leading;
CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
CGPoint origin;
CTFrameGetLineOrigins(_frame, CFRangeMake(i, 1), &origin);
Problems:
- ascent obtained from CTLineGetTypographicBounds or ctFont is 13.860352 while self.font.ascender is 16.860352. Where does this 3 point discrepancy come from?
- descent is 4.139648, leading is 0, so ascent + descent + leading = 18, but self.font.lineHeight and line height calculated by subtracting adjacent line origins are 22. Where does this 4 point discrepancy come from?
floor(x+0.5)
'd, in separate steps with separate conditions. Maybe UIFont is taking these changes into consideration internally? – Blau