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

来源:互联网 发布:淘宝开店考试 编辑:程序博客网 时间:2024/06/16 03:22

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.效果图如下,字间距和行间距改变了,展示效果和打印的一致

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

阅读全文
0 0
原创粉丝点击