Проблемы CTLineGetTypographicBounds - PullRequest
6 голосов
/ 14 июня 2011

Код извлекается из SimpleTextInput sampe code , с битовой модификацией.

Создайте фрейм:

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);

строка нижелюбая строка кадра (получить по CTFrameGetLines(_frame)):

CGFloat ascent, descent, leading;
CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
CGPoint origin;
CTFrameGetLineOrigins(_frame, CFRangeMake(i, 1), &origin);

Проблемы:

  1. Восхождение, полученное из CTLineGetTypographicBounds или ctFont, равно 13.860352, а self.font.ascender равно 16.860352.,Откуда происходит это 3-точечное расхождение?
  2. спуск равен 4.139648, начальный - 0, поэтому подъем + спуск + ведущий = 18, но self.font.lineHeight и высота линии, рассчитанные путем вычитания соседних линий, равны 22.Откуда возникает это 4-балльное расхождение?

1 Ответ

0 голосов
/ 31 мая 2013

http://developer.apple.com/library/mac/#documentation/TextFonts/Conceptual/CocoaTextArchitecture/TypoFeatures/TextSystemFeatures.html прочитайте этот документ, и вы узнаете, как рассчитывается высота строки, но я до сих пор не понимаю, почему CTLineGetTypographicBounds получил нулевой лидирующий символ

...