iOS UILabel根据字符串长度自动适应宽度和高度


   //这个frame是初设的,没关系,后面还会重新设置其size。     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];     label.numberOfLines = 0;     label.backgroundColor = [UIColor clearColor];          NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20],};          NSString *str = @"abcdefg你上课可是你的拿到了";     CGSize textSize = [str boundingRectWithSize:CGSizeMake(100, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;          [label setFrame:CGRectMake(100, 100, textSize.width, textSize.height)];     label.textColor = [UIColor greenColor];     label.text = str;     [self.view addSubview:label];


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM