开发者

Problems of CTLineGetTypographicBounds

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.boun开发者_JAVA技巧ds];
_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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜