图文混排--图片跟随文字并且指定显示行数

最终想要的效果:


首先想到图文混排,但是一般的图文混排只适用于不限制文字显示行数,所以就做了一下处理。 

废话不多说直接上解决代码:

///设置图文混排
+ (void)setLineBreakByTruncatingLastLineMiddleForLabel:(UILabel *)label text:(NSString *)text image:(UIImage *)image imageHeight:(CGFloat)imageHeight {
    
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = image;
    CGFloat imgWidth = 0;
    if (image) {
        imgWidth = image.size.width * imageHeight / image.size.height;
    }
    
    if (label.numberOfLines <= 0 ) {
        return;
    }
    NSArray *separatedLines = [text getSeparatedLinesArrayWithFont:label.font rect:label.frame];
    
    NSMutableString *limitedText = [NSMutableString string];
    if (separatedLines.count >= label.numberOfLines) {
        
        for (int i = 0 ; i < label.numberOfLines; i++) {
            if (i == label.numberOfLines - 1) {
                
                NSString *otherText = @"...";
                CGSize otherTextRect = [otherText sizeWithZFont:label.font];
                
                NSString *anotherText = @" ";
                CGSize anotherTextRect = [anotherText sizeWithZFont:label.font];
                
                NSString *lastLineText = separatedLines[label.numberOfLines - 1];
                CGSize lastLineTextRect = [lastLineText sizeWithZFont:label.font];
                CGFloat lastLineLabelW = lastLineTextRect.width;
                
                if (lastLineLabelW > label.frame.size.width - imgWidth - anotherTextRect.width) {
                    lastLineLabelW = label.frame.size.width - imgWidth - otherTextRect.width - anotherTextRect.width;
                }
                
                UILabel *lastLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, lastLineLabelW, MAXFLOAT)];
                lastLineLabel.font = label.font;
                lastLineLabel.text = lastLineText;
                
                NSArray *subSeparatedLines = [lastLineText getSeparatedLinesArrayWithFont:lastLineLabel.font rect:lastLineLabel.frame];
                NSString *subLastLineText = [subSeparatedLines firstObject];
                NSInteger lastLineTextCount = subLastLineText.length;
                if (lastLineTextRect.width > label.frame.size.width - imgWidth - anotherTextRect.width) {
                    lastLineTextCount = subLastLineText.length;
                    [limitedText appendString:[NSString stringWithFormat:@"%@%@%@",[subLastLineText substringToIndex:lastLineTextCount], otherText, anotherText]];
                }else {
                    [limitedText appendString:[subLastLineText substringToIndex:lastLineTextCount]];
                }
            }else{
                [limitedText appendString:separatedLines[i]];
            }
        }
    } else {
        [limitedText appendString:text];
        [limitedText appendString:@" "];
    }
    
    //图片居中显示
    attach.bounds = CGRectMake(0, (label.font.capHeight - imageHeight)/2, imgWidth, imageHeight);
    NSAttributedString *imgStr = [NSAttributedString attributedStringWithAttachment:attach];
    NSMutableAttributedString *abs = [[NSMutableAttributedString alloc] initWithString:limitedText];
    [abs appendAttributedString:imgStr];
    
    label.attributedText = abs;
}
复制代码

将文本根据label宽度分成一行一行并存入一个数组中

- (NSArray *)getSeparatedLinesArrayWithFont:(UIFont *)font rect:(CGRect)rect {
    
    CTFontRef myFont = CTFontCreateWithName((__bridge CFStringRef)([font fontName]), [font pointSize], NULL);
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:self];
    [attStr addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)myFont range:NSMakeRange(0, attStr.length)];
    
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge 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 = (__bridge 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 = [self substringWithRange:range];
        [linesArray addObject:lineString];
    }
    return (NSArray *)linesArray;
}
复制代码

调用代码

detailModel.recvCompany = @"测试测试测试测试测试测试测试测试测试测试试测试测试测试测试测试测试测试测试试测试测试测试测试测试测试测试测试";
UIImage *img = [UIImage imageNamed:@"icon_quzheli"];
[UILabel setLineBreakByTruncatingLastLineMiddleForLabel:self.getCompanyLabel text:[NSString stringWithFormat:@"%@", detailModel.recvCompany] image:img imageHeight:13];
复制代码


转载于:https://juejin.im/post/5bc709d75188255c6140fc7d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值