UILabel自适应高、宽
生活随笔
收集整理的這篇文章主要介紹了
UILabel自适应高、宽
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
根據Label和字體大小自適應高度
- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size {label.numberOfLines = 0;NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])]; CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*2, 9999) options:NSStringDrawingUsesLineFragmentOrigin context:nil]; CGRect labelRect = label.frame; labelRect.size.height = labelStringRect.size.height; label.frame = labelRect; label.attributedText = labelString; }根據Label和字體大小自適應寬度
- (void)getWidthWithLabel:(UILabel *)label andFontSize:(CGFloat)size {label.numberOfLines = 0;NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(0, [labelString length])]; CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(9999, label.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil]; CGRect labelRect = label.frame; labelRect.size.width = labelStringRect.size.width; label.frame = labelRect; label.attributedText = labelString; }轉載于:https://www.cnblogs.com/bugismyalllife/p/4826555.html
總結
以上是生活随笔為你收集整理的UILabel自适应高、宽的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对Git的理解
- 下一篇: 软件工程启程篇章:C#和四则运算生成与运