Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

解决 label 多行间距

使用。 一个是间距,一个是行高,但是鉴于多行,都写的话,就相当于双倍行高了。我就用行高代替间距。

 1 self.textLabel.attributedText = [self getAttributedStringWithString:self.textString lineSpace:0 baselineOffset:10]; 

方法如下

 1 -(NSAttributedString *)getAttributedStringWithString:(NSString *)string lineSpace:(CGFloat)lineSpace baselineOffset:(CGFloat)baselineOffset {
 2     NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
 3     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
 4     paragraphStyle.lineSpacing = lineSpace; // 调整行间距
 5     NSRange range = NSMakeRange(0, [string length]);
 6     [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
 7     // 设置文本偏移量
 8     [attributedString addAttribute:NSBaselineOffsetAttributeName value:@(baselineOffset) range:range];
 9     return attributedString;
10 }

 

posted on 2016-04-22 10:36  Sinner_Yun  阅读(400)  评论(0编辑  收藏  举报

导航