最近项目中需求是:文字最多显示2行,图片跟随文字后面,如下图
![](https://img.haomeiwen.com/i2110475/5f0d42bbdb551814.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];
网友评论