OGeek|极客世界-中国程序员成长平台

标题: iphone - UILabel 中的行数 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:35
标题: iphone - UILabel 中的行数

我有带有自定义单元格的 UITableView,其中包含 uilabels。 我用它来显示评论,它必须有灵活的高度。 我该怎么做? 我有代码,它在 uilabel.text 长度的帮助下计算行数,但这是错误的方法。你有什么想法吗?



Best Answer-推荐答案


使用 UILabel 并将 numberOfLines 设置为零。

然后,为了确保您的标签确实适合单元格,请像这样从 NSString UIKit 扩展中获取尺寸;

CGFloat cellWidth = 320.0f; //example width....
labelSize = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(cellWidth, FLT_MAX)];

来自 NSString 引用; sizeWithFont:constrainedToSize:

Returns the size of the string if it were rendered and constrained to the specified size.

- (CGSize)sizeWithFontUIFont *)font constrainedToSizeCGSize)size

Parameters font The font to use for computing the string size. size The maximum acceptable size for the string. This value is used to calculate where line breaks and wrapping would occur. Return Value The width and height of the resulting string’s bounding box.

关于iphone - UILabel 中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8285954/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4