iOS UILabel自定义行间距

时间:2024-11-08 14:34:38
        NSString *hintStr = @"输入材料标题搜索材料\n注:可根据材料序号直接搜索, 如TPO23";

        CGSize size = [toolset returnTextStringSizeWithString:hintStr withBoundSize:CGSizeMake(Main_Screen_Width - , MAXFLOAT) withFont:FZHei()];

        self.mHintLabelLayoutH.constant = size.height + ;

        [self.mHitLabel setNeedsLayout];

        NSMutableAttributedString *prefixAttr = [[NSMutableAttributedString alloc] initWithString:hintStr];

        [prefixAttr addAttribute:NSFontAttributeName value:FZHei() range:NSMakeRange(, hintStr.length)];

        [prefixAttr addAttribute:NSForegroundColorAttributeName value:ColorHui range:NSMakeRange(, hintStr.length)];

        [prefixAttr addAttribute:NSForegroundColorAttributeName value:HexRGB(0xc0c0c0) range:NSMakeRange(, )];

        [prefixAttr addAttribute:NSFontAttributeName value:FZHei() range:NSMakeRange(, )];

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

        [paragraphString setLineSpacing:];    //设置行间距

        [prefixAttr addAttribute:NSParagraphStyleAttributeName value:paragraphString range:NSMakeRange(, hintStr.length)];

        self.mHitLabel.attributedText = prefixAttr;
 UILabel *dataLabel = [[UILabel alloc] init];
dataLabel.backgroundColor = [UIColor clearColor];
dataLabel.text = @"我是文字文字文字文字文字文字文字文字文字文字文字";
dataLabel.textColor = RGBACOLOR(, , , );
dataLabel.font = [UIFont fontWithName:@"Arial" size:];
dataLabel.numberOfLines = ;
[backImage addSubview:dataLabel]; //富文本设置文字行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
paragraphStyle.lineSpacing = ;
NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:], NSParagraphStyleAttributeName:paragraphStyle};
dataLabel.attributedText = [[NSAttributedString alloc]initWithString:dataLabel.text attributes:attributes]; //获取设置文本间距以后的高度
CGRect fram = [dataLabel.attributedText boundingRectWithSize:CGSizeMake(, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];