iOS中求出label中文字的行数和每一行的内容,带自定义行间距

1.引入头文件#import
2.如果没设置字间距,行间距,断行模式,使用使用默认的,求出label中文字的行数和每一行的内容,使用下面方法:
- (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label{

    NSString *text = [label text];
    UIFont *font = [label font];
    CGRect rect = [label frame];

    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)myFont range:NSMakeRange(0, attStr.length)];
    CFRelease(myFont);
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];
    for (id line in lines) {
        CTLineRef lineRef = (__bridge  CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        NSString *lineString = [text substringWithRange:range];
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));
        //NSLog(@"''''''''''''''''''%@",lineString);
        [linesArray addObject:lineString];
    }

    CGPathRelease(path);
    CFRelease( frame );
    CFRelease(frameSetter);
    return (NSArray *)linesArray;
}
3.如果自定义行间距,字间距,段子模式等设置
3.1设置这些参数,封装到一个助手类里面了
+ (NSDictionary *)setTextLineSpaceWithString:(NSString *)string
                           withLineBreakMode:(NSLineBreakMode)lineBreakMode
                               withAlignment:(NSTextAlignment)alignment
                                    withFont:(UIFont *)font
                               withLineSpace:(CGFloat)lineSpace
                         withTextlengthSpace:(NSNumber *)textlengthSpace
                        andParagraphSpaceing:(CGFloat)paragraphSpacing {

    // 1. 创建样式对象
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    // 2. 每行容纳字符的宽度
    style.lineBreakMode = lineBreakMode;
    // 3. 对齐方式
    style.alignment = alignment;
    // 4. 设置行间距
    style.lineSpacing = lineSpace;
    // 5. 连字符号链接
    style.hyphenationFactor = 1.0f;
    // 6. 首行缩进
    style.firstLineHeadIndent = 0.f;
    // 7. 段间距
    style.paragraphSpacing = paragraphSpacing;
    // 8. 段前间距
    style.paragraphSpacingBefore = 0.0f;
    // 9. 除首行之外其他行缩进
    style.headIndent = 0.0f;
    // 10. 每行容纳字符的宽度
    style.tailIndent = 0.0f;
    NSDictionary *dict = @{NSFontAttributeName : font,
                           NSParagraphStyleAttributeName : style,
                           NSKernAttributeName : textlengthSpace,
                           };
    return dict;
}
3.2设置label
CGFloat contentLabelWidth = kScreenWidth - 36 * ScaleNumberWidth;
    CGFloat kContentFontSize  = 14.f;
    CGFloat kContentLineSpace = 1.5f;
    CGFloat kContentTextLengthSpage = 0.8f;
    contentLabel.size = CGSizeMake(contentLabelWidth, 1);
    contentLabel.font = CRFontType5(kContentFontSize);
    contentLabel.numberOfLines = 0;
    NSDictionary *contentDict = [CommonClass setTextLineSpaceWithString:content withLineBreakMode:(NSLineBreakByCharWrapping) withAlignment:(NSTextAlignmentLeft) withFont:CRFontType5(kContentFontSize) withLineSpace:kContentLineSpace  withTextlengthSpace:[NSNumber numberWithFloat:kContentTextLengthSpage] andParagraphSpaceing:0];
    contentLabel.attributedText = [[NSMutableAttributedString alloc] initWithString:content attributes:contentDict];
    [contentLabel sizeToFit];
    contentLabel.center = CGPointMake(kScreenWidth / 2, CGRectGetMaxY(priceLabel.frame) + 20 * ScaleNumberHeight + contentLabel.height / 2);
3.3在自定义label参数的情况下,计算label中文字的行数和每一行的内容,修改了NSMutableAttributedString参数
- (NSArray *)getLinesArrayOfStringInLabel:(UILabel *)label{

    NSString *text = [label text];
    UIFont *font = [label font];
    CGRect rect = [label frame];

    CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);
    CGFloat  kContentFontSize  = 14.f;
    CGFloat  kContentLineSpace = 1.5f;
    CGFloat  kContentTextLengthSpage = 0.8f;
    NSDictionary *contentDict = [CommonClass setTextLineSpaceWithString:text withLineBreakMode:(NSLineBreakByCharWrapping) withAlignment:(NSTextAlignmentLeft) withFont:CRFontType5(kContentFontSize) withLineSpace:kContentLineSpace  withTextlengthSpace:[NSNumber numberWithFloat:kContentTextLengthSpage] andParagraphSpaceing:0];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text attributes:contentDict];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge  id)myFont range:NSMakeRange(0, attStr.length)];
    CFRelease(myFont);

    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0,0,rect.size.width,100000));
    CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
    NSArray *lines = ( NSArray *)CTFrameGetLines(frame);
    NSMutableArray *linesArray = [[NSMutableArray alloc]init];

    for (id line in lines) {

        CTLineRef lineRef = (__bridge  CTLineRef )line;
        CFRange lineRange = CTLineGetStringRange(lineRef);
        NSRange range = NSMakeRange(lineRange.location, lineRange.length);
        NSString *lineString = [text substringWithRange:range];
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithFloat:0.0]));
        CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr, lineRange, kCTKernAttributeName, (CFTypeRef)([NSNumber numberWithInt:0.0]));
        [linesArray addObject:lineString];
    }

    CGPathRelease(path);
    CFRelease( frame );
    CFRelease(frameSetter);
    return (NSArray *)linesArray;
}
4.效果图如下,字间距和行间距改变了,展示效果和打印的一致

这里写图片描述
这里写图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值