我正在尝试查找文本字符串的物理像素大小。然后我想使用这个大小来设置 roundRectButton 的长度。我用来获取长度的方法返回了一个 CGSize。我如何将其转换为 CGFloat?或者,也许有人可以建议一种完全不同的方法来完成此任务。
这是我目前的代码:
// Note: tagAsString is a string of Tag names (Example "tag1, tag2, tag3")
// Note: Code to set this is not relevant to the question.
// get length of tagsAsString.
CGSize tagStringLength = [tagsAsString sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:13.0] forWidth:100.0 lineBreakMode:UILineBreakModeTailTruncation];
// size button to fit length of string
// Note: spotTags is a roundRectButton
cell.spotTags.frame = CGRectMake(96, 33, tagStringLength, 25);
// set the title of the roundRectButton to the tag string
[cell.spotTags setTitle:tagsAsString forState:UIControlStateNormal];
正如您在上面看到的,我正在使用 CGRectMake 方法,该方法将 4 个参数作为 CGFloat。然而,我传递了一个导致运行时错误的 CGSize。
最佳答案
CGSize
是一个结构:
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
只需使用 tagStringLength.width
即可获取您关心的数字。
而且我很确定您遇到的应该是编译时错误,而不是运行时错误。
关于iphone - 将 CGSize 转换为 CGFloat 并使用它来调整 RoundRectButton 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9089093/