您的位置:首页 > 移动开发 > IOS开发

设置代码ios中根据文本设置label高度设置代码

2013-04-19 22:20 393 查看
本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~

UILabel * label = [[UILabel alloc] init];
NSString * text = @"hellohellohellohellohellohellohellohellohellohellohellohello ";
label.text = text;
[label setNumberOfLines:0];
UIFont *font = [UIFont fontWithName:@"Arial" size:14];
//设置字体
label.font = font;
CGSize size = CGSizeMake(300, 20000.0f);
size = [text sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
[label setFrame:CGRectMake(10, 0, size.width, size.height)];
[label setFrame:CGRectMake(10, 0, 200, 100)];
[self.view addSubview:label];

这是网上的代码,可以正常运行。但还到遇一个题问,就是这里的label是态动生成的,如果对于view中一个拖出来的label然后在controller中连接一个对应的属性,这个label的setFrame一直效无,这时可以点到size inspector中,找到constraints中的height equals,点击前面的齿轮图案,选择select and edit,在height constraints中将Priority的值设置的小一些可即。

文章结束给大家分享下程序员的一些笑话语录: 看到有人回帖“不顶不是中国人”,他的本意是想让帖子沉了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: