Problems of CTLineGetTypographicBounds

喜夏-厌秋 提交于 2019-11-30 04:58:19

问题


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?

回答1:


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



来源:https://stackoverflow.com/questions/6338116/problems-of-ctlinegettypographicbounds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!