gpt4 book ai didi

iphone - 如何计算 CATextLayer 字符串的边界框?

转载 作者:可可西里 更新时间:2023-11-01 03:27:30 28 4
gpt4 key购买 nike

乍一看我的问题看起来很简单,但似乎我真的找不到解决方案。它是这样的:我想计算 CATextLayer 字符串的边界框。这是我所做的:

CATextLayer *textLayer = [CATextLayer layer];
textLayer.frame = CGRectMake(80, 0.0f, 36.0f, 18.0f);
textLayer.string = @"12";
textLayer.fontSize = [UIFont systemFontSize];
textLayer.foregroundColor = [UIColor whiteColor].CGColor;

NSLog(@"(width,height)=(%f,%f)",
[textLayer.string sizeWithFont:textLayer.font].width,
[textLayer.string sizeWithFont:textLayer.font].height);

问题是输出总是:(width,height) = (8.000000,0.000000)

最佳答案

使用sizeWithFont:constrainedToSize:lineBreakMode:

[someString sizeWithFont:yourFont
constrainedToSize:CGSizeMake(maxWidthYouSpecify, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];

关于iphone - 如何计算 CATextLayer 字符串的边界框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9538515/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com