
假设相同的属性和选项,字符串“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: @H_502_4@ @H_502_4@ @H_502_4@ @H_502_4@ 总结
NsstringDrawingUsesDeviceMetricsUse the image glyph bounds (instead of the typographic bounds) when computing layout.
以上是内存溢出为你收集整理的ios – NSString boundingRectWithSize返回不必要的高度全部内容,希望文章能够帮你解决ios – NSString boundingRectWithSize返回不必要的高度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)