iOS UILabel设置行高代码解析

这篇文章主要为大家详细介绍了iOS UILabel中设置行高的代码解析,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

UILabel *tileLabel = [[UILabel alloc] init];

    tileLabel.numberOfLines = 0;

    tileLabel.backgroundColor = [UIColor clearColor];

    tileLabel.text = @"调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头调整行间距云头";

    tileLabel.font = [UIFont systemFontOfSize:11.0f];

    tileLabel.textColor = [Tools colorWithHex:0x787878];


    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:tileLabel.text];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setLineSpacing:18];//调整行间距

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [tileLabel.text length])];

    tileLabel.attributedText = attributedString;


    NSDictionary *attributes = @{NSFontAttributeName:tileLabel.font, NSParagraphStyleAttributeName:paragraphStyle.copy};

    CGFloat height = [tileLabel.text boundingRectWithSize:CGSizeMake(self.view.width-32, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size.height ;

//    CGFloat height =[self changeStationWidth:tileLabel.text anWidthTxtt:self.view.width-32 anfont:11];

    tileLabel.frame = CGRectMake(16, 0, self.view.width-32, height);


    [self.view addSubview:tileLabel];

关于iOS UILabel设置行高就分享到这里了,希望以上内容可以对大家有一定的参考价值,可以学以致用。如果喜欢本篇文章,不妨把它分享出去让更多的人看到。