ken*_*nyc 18 macos cocoa objective-c nsstring ios
当使用[NSString boundingRectWithSize:options:attributes]返回的rect的大小比我期望的某些字符串高.返回的高度似乎表示具有给定属性的字符串的最大可能高度,而不是字符串本身的高度.
假设相同的属性和选项,字符串" cars" 返回的高度与字符串" " 返回的高度相同ÉTAS-UNIS(注意E上的重音).
我本来希望boundingRectWithSize只考虑给定字符串中的字符,在我看来,它会为字符串" cars" 返回一个较短的高度.
在附带的截图中,我填写了返回的矩形,boundingRectWithSize并用红色概述了我认为边界矩形应该是什么.矩形的宽度与我预期的差不多,但高度比我预期的要高得多.这是为什么?

示例代码:
NSRect boundingRect = NSZeroRect;
NSSize constraintSize = NSMakeSize(CGFLOAT_MAX, 0);
NSString *lowercaseString = @"cars";
NSString *uppercaseString = @"ÉTAS-UNIS";
NSString *capitalizedString = @"Japan";
NSFont *drawingFont = [NSFont fontWithName:@"Georgia" size:24.0];
NSDictionary *attributes = @{NSFontAttributeName : drawingFont, NSForegroundColorAttributeName : [NSColor blackColor]};
boundingRect = [lowercaseString boundingRectWithSize:constraintSize options:0 attributes:attributes];
NSLog(@"Lowercase rect: %@", NSStringFromRect(boundingRect));
boundingRect = [uppercaseString boundingRectWithSize:constraintSize options:0 attributes:attributes];
NSLog(@"Uppercase rect: %@", NSStringFromRect(boundingRect));
boundingRect = [capitalizedString boundingRectWithSize:constraintSize options:0 attributes:attributes];
NSLog(@"Capitalized rect: %@", NSStringFromRect(boundingRect));
Run Code Online (Sandbox Code Playgroud)
输出:
Lowercase rect: {{0, -6}, {43.1953125, 33}}
Uppercase rect: {{0, -6}, {128.44921875, 33}}
Capitalized rect: {{0, -6}, {64.5, 33}}
Run Code Online (Sandbox Code Playgroud)
@omz仍然因为我的工作而受到赞誉.他的回答让我更多地看看CoreText,因为我假设boundingRectWithSize最终会调用CoreText函数.
在WWDC 2012的Session 226中,有一整节专门用于计算一行的度量,令我惊讶的是,他们讨论了一个名为的新CoreText函数CTLineGetBoundsWithOptions.
据我所知,该方法仅记录在文档中CTLine.h和CoreText Changes文档中.在文档中进行常规搜索时,它肯定不会出现(对我而言).
但它似乎工作,在我的测试中,它返回与boundingRectWithSize我系统上安装的所有字体完全相同的结果.更好的是,它似乎快了近2倍boundingRectWithSize.
正如WWDC视频所提到的,为什么你需要计算字符串的边界而不考虑行高等问题,这有点模糊,但如果你确实需要这样做,那么我认为这可能是最好的方法.使用.一如既往,YMMV.
粗略的示例代码:
NSFont *font = [NSFont systemFontOfSize:13.0];
NSDictionary *attributes = @{(__bridge NSString *)kCTFontAttributeName : font};
NSAttributedString *attributeString = [[NSAttributedString alloc] initWithString:self.text attributes:attributes];
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)attributeString);
CGRect bounds = CTLineGetBoundsWithOptions(line, kCTLineBoundsUseGlyphPathBounds);
CFRelease(line);
return NSRectFromCGRect(bounds);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16652 次 |
| 最近记录: |