實(shí)例如下:
NSString *contentStr = @"總以為,在最初的地方,有一個(gè)最原來(lái)的我,就也會(huì)有一個(gè)最原來(lái)的你"; UILabel *tempLabel = [[UILabel alloc] init]; //設(shè)置背景顏色 tempLabel.backgroundColor = [UIColor redColor]; //設(shè)置內(nèi)容 tempLabel.text = contentStr; //設(shè)置字體顏色 tempLabel.textColor = [UIColor whiteColor]; //設(shè)置字體大小 tempLabel.font = [UIFont systemFontOfSize:15.0]; //設(shè)置文本是否多行顯示 tempLabel.numberOfLines = 0; tempLabel.lineBreakMode = NSLineBreakByWordWrapping; //設(shè)置UILabel根據(jù)文字自適應(yīng)高度 CGSize size = [tempLabel sizeThatFits:CGSizeMake(100, MAXFLOAT)]; //設(shè)置frame tempLabel.frame = CGRectMake(50, 100, 100, size.height); [self.view addSubview:tempLabel]; //設(shè)置label的行間距 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:8]; [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentStr length])]; [tempLabel setAttributedText:attributedString]; [tempLabel sizeToFit];
以上這篇iOS 設(shè)置UILabel的行間距并自適應(yīng)高度的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注