自動(dòng)計(jì)算Label高度、寬度

自動(dòng)計(jì)算高度和寬度的關(guān)鍵點(diǎn)無外乎三個(gè)因素,文字內(nèi)容,寬度或高度,再者就是字號(hào),通過這三個(gè)因素,去自動(dòng)計(jì)算對(duì)應(yīng)的高度或?qū)挾?/code>

自動(dòng)計(jì)算高度




-(CGRect)contentRectWithString:(NSString *)string labelWidth:(CGFloat)labelWidth AndStringFont:(int)fontSize
{
    CGRect tempRect = [string boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT)
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                        attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize],NSFontAttributeName, nil] context:nil];
    
    return tempRect;
}


自動(dòng)計(jì)算寬度


-(CGRect)contentRectWithString:(NSString *)string labelHeight:(CGFloat)labelHeight AndStringFont:(int)fontSize
{
    CGRect tempRect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, labelHeight)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize],NSFontAttributeName, nil] context:nil];
    
    return tempRect;
}

示例代碼

#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *myLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.myLabel = (UILabel *)[self.view viewWithTag:3];
    
    [self.view addSubview:self.myLabel];
    
    self.myLabel.numberOfLines = 0;
    
    NSString *str = @"豫章故郡,洪都新府。星分翼軫,地接衡廬。襟三江而帶五湖,控蠻荊而引甌越。物華天寶,龍光射牛斗之墟;人杰地靈,徐孺下陳蕃之榻。雄州霧列,俊采星馳。臺(tái)隍枕夷夏之交,賓主盡東南之美。都督閻公之雅望,棨戟遙臨;宇文新州之懿范,襜帷暫駐。十旬休假,勝友如云;千里逢迎,高朋滿座。騰蛟起鳳,孟學(xué)士之詞宗;紫電青霜,王將軍之武庫。家君作宰,路出名區(qū);童子何知,躬逢勝餞。時(shí)維九月,序?qū)偃铩A仕M而寒潭清,煙光凝而暮山紫。儼驂騑于上路,訪風(fēng)景于崇阿。臨帝子之長洲,得仙人之舊館。層巒聳翠,上出重霄;飛閣流丹,下臨無地。鶴汀鳧渚,窮島嶼之縈回;桂殿蘭宮,列岡巒之體勢(shì)。披繡闥,俯雕甍,山原曠其盈視,川澤紆其駭矚。閭閻撲地,鐘鳴鼎食之家;舸艦迷津,青雀黃龍之舳。云銷雨霽,";
    
    
    CGRect labelRect = [self contentRectWithString:str labelWidth:200.0 AndStringFont:15.0];

    float height = labelRect.size.height;

    [self.myLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(height);
        
    }];
    
    self.myLabel.text = str;
    

}

-(CGRect)contentRectWithString:(NSString *)string labelWidth:(CGFloat)labelWidth AndStringFont:(int)fontSize
{
    CGRect tempRect = [string boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT)
                                           options:NSStringDrawingUsesLineFragmentOrigin
                                        attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:fontSize],NSFontAttributeName, nil] context:nil];
    
    return tempRect;
}

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

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

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,154評(píng)論 1 92
  • 選擇qi:是表達(dá)式 標(biāo)簽選擇器 類選擇器 屬性選擇器 繼承屬性: color,font,text-align,li...
    wzhiq896閱讀 2,119評(píng)論 0 2
  • 選擇qi:是表達(dá)式 標(biāo)簽選擇器 類選擇器 屬性選擇器 繼承屬性: color,font,text-align,li...
    love2013閱讀 2,437評(píng)論 0 11
  • 翻譯自“Auto Layout Guide”。 1 入門 1.1 理解自動(dòng)布局 自動(dòng)布局根據(jù)視圖層級(jí)結(jié)構(gòu)中視圖上的...
    lakerszhy閱讀 3,959評(píng)論 3 26
  • *左心 蒲公英籠罩著夜色 夜色籠罩著城市 于是城市的效果 星芒閃爍 這個(gè)一直在說話的城市 有酒有燒烤...
    5b8fd3f74fc5閱讀 247評(píng)論 0 0

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