Set the line spacing and text indent of UILabel

UILabel line spacing settings

The default line spacing for UILabels is very small, and a line spacing is required most of the time. The implementation is also very simple, basically using the lineSpacing property of NSMutableParagraphStyle. But careful people found that the setting did not achieve the expected effect, but a little larger than the required spacing, mainly because of the UILabel's text size (label.font.pointSize) and the height of each line (label.font. lineHeight) is not overlapping, but a part of the blank, so when setting the line spacing, the height of this part of the blank must be subtracted to achieve the standard line spacing.

UILabel *label = [[UILabel alloc]init];
[self.view addSubview:label];
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:14];
label.backgroundColor = [UIColor yellowColor];

NSString *str = @"在中国枸杞有很多民间叫法,如苟起子、枸杞红实、甜菜子、西枸杞、狗奶子、红青椒、枸蹄子、枸杞果、地骨子、枸茄茄、红耳坠、血枸子、枸地芽子、枸杞豆、血杞子、津枸杞。在宁夏枸杞主产区,宁夏省中宁县,农民们习惯称呼枸杞为“茨”,茨即蒺藜。这是由于野生枸杞与蒺藜相似,常被混采作烧柴,在民间把“茨”当作枸杞的俗名叫惯了。在中宁农村,枸杞园称为茨园,拘杞树称为茨树,枸杞枝称为茨条。于是,盛产枸杞的中宁农村又被称为茨乡,富有中宁地方色彩的文化也往往被冠以茨乡的称号,如茨乡戏、茨乡歌谣等。但是,在药材领域里,枸杞即枸杞子,不用茨果,茨实等称谓。";

NSMutableAttributedString *attributeStr =[[NSMutableAttributedString alloc] initWithString:str];

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

// 设置行间距为 要求的行间距减去每行文字的留白部分
style.lineSpacing = 10 - (label.font.lineHeight - label.font.pointSize);

NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:14], NSParagraphStyleAttributeName: style};

[attributeStr addAttributes:dict range:NSMakeRange(0, attributeStr.length)];

// 计算文字的宽高
CGRect rect = [str boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-40, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];

label.frame = CGRectMake(20, 100, ceilf(rect.size.width), ceilf(rect.size.height));
label.attributedText = attributeStr;
label.lineBreakMode = NSLineBreakByTruncatingTail;

The text content of UILabel is indented

The text indent attribute is not implemented in the default properties of UILabel, so we need to use UILabel to limit its drawing area when drawing text to achieve text indentation

Inherit UILabel and write a subclass of ContentInsetsLabel, add an indented property contentInsets, and then pass in the restricted area by overriding UILabel's drawTextInRect method to achieve text indentation

@interface ContentInsetsLabel : UILabel

// 文字上左下右的缩进值
@property (assign, nonatomic) UIEdgeInsets contentInsets;

@end

@implementation ContentInsetsLabel

// 重写方法传入缩进后的区域
- (void)drawTextInRect:(CGRect)rect {
    [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.contentInsets)];
}

@end

When text indentation is required, directly assign a value to the contentInsets property to achieve text indentation. The calling method is as follows

ContentInsetsLabel *label = [[ContentInsetsLabel alloc]init];
[self.view addSubview:label];
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:14];
label.backgroundColor = [UIColor yellowColor];
label.contentInsets = UIEdgeInsetsMake(10, 10, 10, 10);

NSString *str = @"在中国枸杞有很多民间叫法,如苟起子、枸杞红实、甜菜子、西枸杞、狗奶子、红青椒、枸蹄子、枸杞果、地骨子、枸茄茄、红耳坠、血枸子、枸地芽子、枸杞豆、血杞子、津枸杞。在宁夏枸杞主产区,宁夏省中宁县,农民们习惯称呼枸杞为“茨”,茨即蒺藜。这是由于野生枸杞与蒺藜相似,常被混采作烧柴,在民间把“茨”当作枸杞的俗名叫惯了。在中宁农村,枸杞园称为茨园,拘杞树称为茨树,枸杞枝称为茨条。于是,盛产枸杞的中宁农村又被称为茨乡,富有中宁地方色彩的文化也往往被冠以茨乡的称号,如茨乡戏、茨乡歌谣等。但是,在药材领域里,枸杞即枸杞子,不用茨果,茨实等称谓。";

// 注意:在计算文字高度的时候传入的CGSize是文字缩进之后的最大宽度及高度
CGRect rect = [str boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-40, CGFLOAT_MAX) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]} context:nil];

// 注意:计算出来的rect是文字缩进后的自适应宽度和高度 所以在设置文字frame的时候要将设置缩进的边距加回来
label.frame = CGRectMake(10, 250, ceilf(rect.size.width+20), ceilf(rect.size.height+20));

label.text = str;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326054123&siteId=291194637