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