ios – NSString boundingRectWithSize返回不必要的高度

ios – NSString boundingRectWithSize返回不必要的高度,第1张

概述当使用[NSString boundingRectWithSize:options:attributes]时,返回的rect的大小比我期望的某些字符串高。返回的高度似乎表示具有给定属性的字符串的最大可能高度,而不是字符串本身的高度。 假设相同的属性和选项,字符串“cars”返回的高度与字符串“ÉTAS-UNIS”返回的高度相同(注意E上的重音)。 我会期望boundingRectWithSize只 @H_502_4@ 当使用[Nsstring boundingRectWithSize:options:attributes]时,返回的rect的大小比我期望的某些字符串高。返回的高度似乎表示具有给定属性的字符串的最大可能高度,而不是字符串本身的高度。

假设相同的属性和选项,字符串“cars”返回的高度与字符串“ÉTAS-UNIS”返回的高度相同(注意E上的重音)。

我会期望boundingRectWithSize只考虑给定字符串中的字符,这在我看来会让字符串“cars”返回一个较短的高度。

在附带的屏幕截图中,我已经填写了从boundingRectWithSize返回的rect,并以红色概括了我本来应该假设的边界。直角的宽度与我预期的相差很大,但高度比预期的要高得多。这是为什么?

示例代码:

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

输出:

Lowercase rect: {{0,-6},{43.1953125,33}}Uppercase rect: {{0,{128.44921875,33}}CAPItalized rect: {{0,{64.5,33}}
@H_502_4@解决方法 您可能希望在选项中使用NsstringDrawingUsesDeviceMetrics。从 docs:

NsstringDrawingUsesDeviceMetrics

Use the image glyph bounds (instead of the typographic bounds) when computing layout.

@H_502_4@ @H_502_4@ @H_502_4@ @H_502_4@ 总结

以上是内存溢出为你收集整理的ios – NSString boundingRectWithSize返回不必要的高度全部内容,希望文章能够帮你解决ios – NSString boundingRectWithSize返回不必要的高度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1036589.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存