【代码笔记】iOS-UILabel根据内容自动调整高度

来源:互联网 发布:淘宝店铺爆款神器软件 编辑:程序博客网 时间:2024/06/08 10:45

一,效果图。

二,代码。

复制代码
- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        //根据内容自动调整高度    NSString *str = @"公元前3000年,印度河流域的居民的数字使用就已经比较普遍,居民们采用了十进位制的计算法。";    UIFont *font = [UIFont systemFontOfSize:13];    CGSize size = CGSizeMake(200,2000);    CGRect labelRect = [str boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)  attributes:[NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName] context:nil];        //UILabel    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50,100, labelRect.size.width, labelRect.size.height)];    label.text = str;    label.backgroundColor=[UIColor redColor];    label.font = [UIFont systemFontOfSize:13];    label.numberOfLines=0;    [self.view addSubview: label];}
复制代码
阅读全文
0 0