iOS 動(dòng)態(tài)計(jì)算文本寬高總結(jié)

這里只針對iOS 7后的動(dòng)態(tài)計(jì)算文本寬高方法進(jìn)行總結(jié),也就是下文中的方法:

// for NSString
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(nullable NSDictionary<NSString *, id> *)attributes context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 7_0);

以及

// for NSAttributedString
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 6_0);
首先,我們先來講講NSString的動(dòng)態(tài)計(jì)算寬高的方法
    NSString* str = @"這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本";
    self.label = [[UILabel alloc]init];
    
    /************************這幾項(xiàng)會(huì)影響最后計(jì)算出來的結(jié)果********************************/
    self.label.lineBreakMode = NSLineBreakByCharWrapping;
    self.label.font = [UIFont systemFontOfSize:15];
//    self.label.textAlignment = NSTextAlignmentLeft;
    self.label.numberOfLines = 0;
    /******************************************************************************/
   
    self.label.layer.borderWidth = 1;
    self.label.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    self.label.textColor = [UIColor darkGrayColor];
    self.label.text = str;
    NSDictionary* dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGRect rect = [self.label.text boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
    self.label.frame = CGRectMake(100, 100, rect.size.width, rect.size.height);
    [self.view addSubview:self.label];
效果圖

特別注意上面所說的幾項(xiàng)影響最后計(jì)算出來的寬高的因素,例如一開始我就因?yàn)闆]有指定label的字體大小,導(dǎo)致顯示一直有問題,找了半天才找到。

其次,我們先來講講NSAttributedString的動(dòng)態(tài)計(jì)算寬高的方法
//設(shè)置一個(gè)段落格式
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineSpacing = 10;
paragraph.firstLineHeadIndent = 20.f;

NSDictionary* dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor darkGrayColor],NSParagraphStyleAttributeName:paragraph};

self.label = [[UILabel alloc]init];
self.label.layer.borderWidth = 1;
self.label.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self.label.lineBreakMode =  NSLineBreakByCharWrapping;
self.label.numberOfLines = 0;
self.label.text = @"這是一個(gè)測試文本\n這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本這是一個(gè)測試文本";
NSRange allRange = NSMakeRange(0, self.label.text.length);
[self.view addSubview:self.label];

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self.label.text];

[attrStr addAttributes:dic range:allRange];
//NSMutableAttributedString的動(dòng)態(tài)計(jì)算寬高方法
CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];

self.label.frame = CGRectMake(100, 100, rect.size.width, rect.size.height);

self.label.attributedText = attrStr;
效果圖 ![Uploading 屏幕快照 2017-05-25 上午11.31.29_101947.png . . .]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容