美文网首页iOS点点滴滴
图文混排--图片跟随文字并且指定显示行数

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

作者: MJBaby | 来源:发表于2018-10-17 17:25 被阅读32次

最近项目中需求是:文字最多显示2行,图片跟随文字后面,如下图


1.jpg

首先想到图文混排,但是一般的图文混排只适用于不限制文字显示行数,所以就做了一下处理。
废话不多说直接上解决代码

///设置图文混排
+ (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];

相关文章

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

    最近项目中需求是:文字最多显示2行,图片跟随文字后面,如下图 首先想到图文混排,但是一般的图文混排只适用于不限制文...

  • 利用YYLabel 进行图文混排+高度计算

    利用YYLabel 进行图文混排+高度计算 1、项目需求: 用一个控件显示图片和文字,并且依据图片和文字动态计算控...

  • Flutter仿朋友圈多行文字展开收起

    Flutter实现类似朋友圈文字显示控件功能:1.文字较少时,直接显示原文本。2.文字超过指定的最大行数时,默认只...

  • 动态图文混排

    上面的播放图标是动态的,文字第一行需在它后面显示,其余行数则要与图标左对齐显示。如果图标是静态的,实现以上图文混排...

  • 小程序图片预占位加载

    手机QQ 浏览器大家都知道,图文混排最常见了,一打开文章文字显示,图片是一个预占位然后显示,且图片是按照预占位大小...

  • 聊天窗口小技术点

    文字输入UITextView 随文字行数变高 发送图片消息显示的效果(如上图所示) 参考资料: https://...

  • iOS开发常见问题-图文混排

    图文混排 一个Label显示多行不同字体的文字

  • scroll-view的scroll-into-view属性

    页面中需要列表展示图片,并且刚进入展示页面的时候,不是直接显示顶部,而是显示列表中指定图片的位置。正好 scrol...

  • 动态改变UIWebView的高度

    需求界面由两部分构成,上部分显示图片,下部分显示文字,由于服务器是分开返回图片及HTML,并且滚动文字的时候,图片...

  • iOS CoreText(二)

    这篇主要讲利用coreText来实现图文混排和点击事件 图文混排思路 文字的绘制只需要知道文字的大小就够了,而图片...

网友评论

  • 阿木摄影:一行能上下居中显示吗?
    MJBaby:这主要是图文混排 不是文字排版 没有关注这个问题

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

本文链接:https://www.haomeiwen.com/subject/rcurzftx.html