圖文混排--圖片跟隨文字并且指定顯示行數(shù)

最近項目中需求是:文字最多顯示2行,圖片跟隨文字后面,如下圖


1.jpg

首先想到圖文混排,但是一般的圖文混排只適用于不限制文字顯示行數(shù),所以就做了一下處理。
廢話不多說直接上解決代碼

///設(shè)置圖文混排
+ (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;
}

將文本根據(jù)label寬度分成一行一行并存入一個數(shù)組中

- (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;
}
分類方法
- (CGSize)sizeWithZFont:(UIFont *)font {
    NSDictionary *attributeDict = @{NSFontAttributeName:font};
    CGSize size = [self sizeWithAttributes:attributeDict];
    return CGSizeMake(ceil(size.width), ceil(size.height));
}

調(diào)用代碼

NSString *recvCompany = @"測試測試測試測試測試測試測試測試測試測試試測試測試測試測試測試測試測試測試試測試測試測試測試測試測試測試測試";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 92, 20)];
label.textColor = blackTextColor;
label.font = [UIFont systemFontOfSize:14];
label.textAlignment = NSTextAlignmentLeft;
self.getCompanyTitleLabel = label;
UIImage *img = [UIImage imageNamed:@"icon_quzheli"];
[UILabel setLineBreakByTruncatingLastLineMiddleForLabel:self.getCompanyLabel text:[NSString stringWithFormat:@"%@", detailModel.recvCompany] image:img imageHeight:13];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容