ios unlabe 显示html,iOS UILabel显示更多

UI设计的设计图,新闻内容最多展示3行,超过2行显示 “显示更多”

利用coreText,计算出每一行要显示的文字,

code如下:

#import

NS_ASSUME_NONNULL_BEGIN

@interface UILabel (ReadMore)

-(void)setReadMoreLabelContentMode:(CGFloat)labelWidth;

@end

NS_ASSUME_NONNULL_END

#import "UILabel+ReadMore.h"

#import

@implementation UILabel (ReadMore)

- (void)setReadMoreLabelContentMode:(CGFloat)labelWidth{

NSArray *contents = [self getLinesArrayOfLabelRows:labelWidth];

if (contents.count <= 2) {

self.userInteractionEnabled = NO; // 如果一行就不显示查看更多,同时取消手势响应

return;

}

self.userInteractionEnabled=YES;

//一个中文也算1个长度,如果末尾要截取的是英文,则“查看更多”会显示不全,所以截取的长度要长一些

NSUInteger cutLength = 8; // 后面截取的长度

NSMutableString *contentText = [[NSMutableString alloc] init];

for (NSInteger i = 0; i < self.numberOfLines; i++) {

if (i == self.numberOfLines - 1) { // 最后一行 进行处理加上.....

NSString *lastLineText = [NSString stringWithFormat:@"%@",contents[i]];

NSUInteger lineLength = lastLineText.length;

if (lineLength > cutLength) {

lastLineText = [lastLineText substringToIndex:(lastLineText.length - cutLength)];

}

[contentText appendString:[NSString stringWithFormat:@"%@...",lastLineText]];

} else {

[contentText appendString:contents[i]];

}

}

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];

NSDictionary *dictionary = @{

NSForegroundColorAttributeName : self.textColor,

NSFontAttributeName : self.font,

NSParagraphStyleAttributeName : style

};

NSMutableAttributedString *mutableAttribText = [[NSMutableAttributedString alloc] initWithString:[contentText stringByAppendingString:@" 查看更多"] attributes:dictionary];

[mutableAttribText addAttributes:@{

NSFontAttributeName : [UIFont systemFontOfSize:12.f],

NSForegroundColorAttributeName :[UIColor colorWithHexString:@"CEA779"]

} range:NSMakeRange(contentText.length, 6)];

self.attributedText = mutableAttribText;

}

// 获取 Label 每行内容 得到一个数组

- (NSArray *)getLinesArrayOfLabelRows:(CGFloat)labelWidth{

//CGFloat labelWidth = self.frame.size.width;

//如果使用autolayout,labelWidth会为0

NSString *text = [self text];

UIFont *font = [self font];

if (text == nil) {

return nil;

}

CTFontRef myFont = CTFontCreateWithName(( CFStringRef)([font fontName]), [font pointSize], NULL);

NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;

[attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attStr.length)];

[attStr addAttribute:(NSString *)kCTFontAttributeName

value:(__bridge id)myFont

range:NSMakeRange(0, attStr.length)];

CFRelease(myFont);

CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(( CFAttributedStringRef)attStr);

CGMutablePathRef path = CGPathCreateMutable();

CGPathAddRect(path, NULL, CGRectMake(0,0,labelWidth,100000));

CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);

NSArray *lines = ( 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];

CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr,

lineRange,

kCTKernAttributeName,

(CFTypeRef)([NSNumber numberWithFloat:0.0]));

CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attStr,

lineRange,

kCTKernAttributeName,

(CFTypeRef)([NSNumber numberWithInt:0.0]));

[linesArray addObject:lineString];

}

CGPathRelease(path);

CFRelease(frame);

CFRelease(frameSetter);

return (NSArray *)linesArray;

}

@end

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值