iPhone - 根据文字调整UILabel宽度

标签 iphone objective-c ios uilabel

如何根据文字调整标签的宽度?如果文本长度较小,我希望标签宽度较小...如果文本长度较小,我希望标签宽度与该文本长度一致。可能吗?

实际上我有两个 UIlabel。我需要把这两个放在附近。但是如果第一个标签的文本太小,就会有很大的差距。我想消除这个差距。

最佳答案

//use this for custom font
CGFloat width =  [label.text sizeWithFont:[UIFont fontWithName:@"ChaparralPro-Bold" size:40 ]].width;

//use this for system font 
CGFloat width =  [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;

label.frame = CGRectMake(point.x, point.y, width,height);

//point.x, point.y -> origin for label;
//height -> your label height; 

关于iPhone - 根据文字调整UILabel宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13152262/

相关文章:

ios - 设置 MPMoviePlayerController 的方向

objective-c - 需要一些调查的 NSString

ios - 检测 iOS 上的向上滑动手势

iphone - 在我的 UIView 中每 5 分钟调用一次方法

iphone - 在 Restkit (iPhone) 中使用 Json 响应

iphone - 使用Oauth2成功认证后获取用户信息

iphone - 在UITableViewView中异步下载多个图像?

ios - 无法让 CALayer 更新其 drawLayer : DURING a bounds animation

ios - 当应用程序终止/终止时如何快速调用服务以获取新更新

ios - 如何在iOS中实现类似Peek Calendar的日历设计?