iOS Label最后一行顯示不全,添加展開

label.png

當(dāng)最后一行顯示不全時(shí),需求有時(shí)候需要改變省略號(hào)的位置,系統(tǒng)并未提供,但我們可以通過 coreText 拿到每行的文字,然后將最后一行文字在指定的地方截?cái)啵倨唇邮÷蕴?hào),話不多說,貼代碼:

FXBreakLineLable.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface FXBreakLineLable : UILabel

- (void)setLineBreakByTruncatingLastLine;

@end

NS_ASSUME_NONNULL_END

FXBreakLineLable.m

#import "FXBreakLineLable.h"
#import <CoreText/CoreText.h>

@implementation FXBreakLineLable

- (void)setLineBreakByTruncatingLastLine {
    
    if ( self.numberOfLines <= 0 ) {
        return;
    }
    NSArray *separatedLines = [self getSeparatedLinesArray];
    
    NSMutableString *limitedText = [NSMutableString string];
    if ( separatedLines.count >= self.numberOfLines ) {
        
        for (int i = 0 ; i < self.numberOfLines; i++) {
            if ( i == self.numberOfLines - 1) {
                FXBreakLineLable *lastLineLabel = [[FXBreakLineLable alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width - 100, MAXFLOAT)];
                lastLineLabel.font = self.font;
                lastLineLabel.text = separatedLines[self.numberOfLines - 1];
                
                NSArray *subSeparatedLines = [lastLineLabel getSeparatedLinesArray];
                NSString *lastLineText = [subSeparatedLines firstObject];
                NSInteger lastLineTextCount = lastLineText.length;
                if ([[lastLineText substringFromIndex:(lastLineTextCount - 1)] isEqualToString:@"\n"]) {
                    lastLineText = [lastLineText substringToIndex:(lastLineText.length - 1)];
                    lastLineTextCount = lastLineTextCount - 1;
                }
                [limitedText appendString:[NSString stringWithFormat:@"%@...",[lastLineText substringToIndex:lastLineTextCount]]];
            } else {
                [limitedText appendString:separatedLines[i]];
            }
        }
        
        
    } else {
        [limitedText appendString:self.text];
    }
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 7;
    NSMutableAttributedString *attText = [[NSMutableAttributedString alloc] initWithString: (( separatedLines.count >= self.numberOfLines ) ? [limitedText stringByAppendingString:@"   全文"] : limitedText)
                                                                       attributes:@{
                                                                                    NSForegroundColorAttributeName : self.textColor,
                                                                                    NSFontAttributeName : self.font,
                                                                                    NSParagraphStyleAttributeName : style
                                                                                    }];
    if ( separatedLines.count >= self.numberOfLines ) {
        [attText addAttributes:@{
                                NSFontAttributeName : [UIFont boldSystemFontOfSize:14.0],
                                NSForegroundColorAttributeName : [UIColor blackColor]
                                } range:NSMakeRange(attText.length - 2, 2)];
    }
    
    self.attributedText = attText;
}

- (NSArray *)getSeparatedLinesArray {
    NSString *text = [self text];
    UIFont   *font = [self font];
    
    CTFontRef myFont = CTFontCreateWithName((__bridge 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)];
    
    CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attStr);
    
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 55, 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 = [text substringWithRange:range];
        [linesArray addObject:lineString];
    }
       
    return (NSArray *)linesArray;
}

@end

當(dāng)前需求是在后端顯示 “全文” 二字,其他需求實(shí)現(xiàn)需要稍作修改;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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