调整UILabel行高间距并返回自定义宽高度

// 获取label文字

NSString *labelString = @"XXXXXXXXXXXXXX";

// 设置自定义宽度或高度

CGSize labelSize = CGSizeMake(ExpectedWidth, CGFLOAT_MAX); // CGSizeMake(CGFLOAT_MAX, ExpectedHeight);

// 获取属性字典

NSDictionary *dic = [self getTextAttributes];

- (NSDictionary *)getTextAttributes {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = 8; //设置行间距
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:kCellFont],
                          NSParagraphStyleAttributeName:paraStyle,
                          NSKernAttributeName:@0.2
                          };
    return dic;
}

// 根据属性计算文字的size

CGSize size = [contentStr boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;

// 设置label的属性字符串

self.label.attributedText = [[NSAttributedString alloc] initWithString:labelString attributes:[self getTextAttributes]];

上一篇:runtime之消息转发


下一篇:SpringBoot进阶教程(二十八)整合Redis事物