Problems of CTLineGetTypographicBounds
Asked Answered
B

1

8

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:

  1. ascent obtained from CTLineGetTypographicBounds or ctFont is 13.860352 while self.font.ascender is 16.860352. Where does this 3 point discrepancy come from?
  2. 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?
Bowne answered 14/6, 2011 at 1:16 Comment(3)
A comment about my bounty -- Even though a diagram like cocoanetics.com/2010/02/understanding-uifont would be the best answer, simply answering the question an0 asked would already be a big help.Burrstone
I don't know why UIFont works the way it does, but I can tell you, for the sake of Googlers, that the 3-point difference in line sizes vs typographic bounds is due to Core Text massaging the line metrics when computing line origins. At least this is the case on OS X... The conditions I have not figured out yet, but I know 0.2*(ascent+descent) is added to the ascent and both the resultant ascent and descent get floor(x+0.5)'d, in separate steps with separate conditions. Maybe UIFont is taking these changes into consideration internally?Blau
Either way, I am guessing we are to simply assume that the size of a CTFrame is not the sum of its CTLines. I don't know if this is explicitly stated anywhere.Blau
V
0

http://developer.apple.com/library/mac/#documentation/TextFonts/Conceptual/CocoaTextArchitecture/TypoFeatures/TextSystemFeatures.html read this doc and you will know how line-height is calculated, but I still don't get why CTLineGetTypographicBounds got a zero leading

Valerie answered 31/5, 2013 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.