iphone - 将 CGSize 转换为 CGFloat 并使用它来调整 RoundRectButton 的大小

标签 iphone objective-c strlen string-length sizewithfont

我正在尝试查找文本字符串的物理像素大小。然后我想使用这个大小来设置 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/

相关文章:

ios - 等待直到NSURLConnection sendAsynchronousRequest完成

ios - 使用加速度计仅调用 void 一次

c - strlen 和释放内存

c - 为什么 strlen 的参数是 "const"?

iphone - 在 iphone 上一次播放一个不同声音的最佳方式?

c# - 只允许 ipad 应用程序用户访问某些文件

c++ - 如何解决出现在 Objective-C++ 而不是 Objective-C 中的链接错误?

ios - CollectionView NSURL session 速度和重新加载

magento - 字符串问题 - 长度不正确 - 包含的某些字符未显示

iphone - 我可以给我的 iPhone 一个域名吗?