目的
项目需求, 筛选之后选择了 YYKit , 其他的三方用起来不能精确的算出 Cell 的高度.
效果图
代码
NSString *string = @"我的世界,因为有你才会美, 因为你是我的世界";
NSRange range1 = [string rangeOfString:@"我的世界"];
NSRange range2 = [string rangeOfString:@"我的世界" options:NSBackwardsSearch];
NSLog(@"range1:%ld--%ld ----- range2:%ld--%ld", range1.location, range1.length, range2.location, range2.length);
- 特别提出: 当文字中出现相同的文字的时候, 寻找他的位置, 这里我是找到他最后一个我需要的字符串, 因为我这个字只在最后出现我才需要它有点击效果.
//假数据
NSDictionary *dic1 = @{@"type":@"0",@"content":@"订单"};
NSDictionary *dic2 = @{@"type":@"3",@"content":@"测试跳转"};
NSDictionary *dic3 = @{@"type":@"0",@"content":@"还未反馈,广告主"};
NSDictionary *dic4 = @{@"type":@"8",@"content":@"差一点是帅哥"};
NSDictionary *dic5 = @{@"type":@"0",@"content":@"提醒你快去"};
NSDictionary *dic6 = @{@"type":@"9",@"content":@"反馈"};
NSDictionary *dic7 = @{@"type":@"0",@"content":@"哦!"};
NSMutableArray * array = [[NSMutableArray alloc]initWithObjects:dic1,dic2,dic3,dic4,dic5,dic6,dic7,nil];
// 拼接字符串
NSMutableArray *strArr =[NSMutableArray arrayWithCapacity:0];
for (NSDictionary *dicttion in array) {
[strArr addObject:dicttion[@"content"]];
}
_pinChuan3 = [strArr componentsJoinedByString:@""];
//设置attributed
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_pinChuan3];
text.font = [UIFont boldSystemFontOfSize:16.0f];
text.lineSpacing = 5;
__weak typeof(self) weakSelf = self;
__block NSString * content1;
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSDictionary * dict = obj;
if ([dict[@"type"] integerValue] !=0) {
content1 = dict[@"content"];
NSLog(@"content---%@",content1);
// NSRange range = [_pinChuan3 rangeOfString:content1];
NSRange range = [_pinChuan3 rangeOfString:content1 options:NSBackwardsSearch];
NSLog(@"range--->%@",NSStringFromRange(range));
[text setTextHighlightRange:range color:[UIColor redColor] backgroundColor:[UIColor clearColor] userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
NSLog(@"点击");
[weakSelf didSelectedDic:dict];
} longPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
NSLog(@"长按");
}];
}
}];
// Set to YYLabel or YYTextView.
YYLabel *label = [YYLabel new];
CGFloat Height = [self getterSpaceLabelHeight:_pinChuan3 withLineSpacing:5 withFont:[UIFont systemFontOfSize:16.0f] withWidth:KScreenWidth - 40];
label.frame = CGRectMake(20, 300,KScreenWidth - 40, Height);
label.attributedText = text;
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:16.0f];
label.backgroundColor = [UIColor lightGrayColor];
[label sizeToFit];
[self.view addSubview:label];
- 创建字段属性的字典, 加入数组, 通过拼接成字符串, 然后通过富文本显示, 通过
- (void)setTextHighlightRange:(NSRange)range color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor userInfo:(NSDictionary *)userInfo tapAction:(YYTextAction)tapAction longPressAction:(YYTextAction)longPressAction ;
对所需的字段添加点击事件.
- (CGFloat)getterSpaceLabelHeight:(NSString*)string withLineSpacing:(CGFloat)lineSpacing withFont:(UIFont*)font withWidth:(CGFloat)width{
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
paraStyle.alignment = NSTextAlignmentLeft;
paraStyle.lineSpacing = lineSpacing;
paraStyle.hyphenationFactor = 1.0;
paraStyle.firstLineHeadIndent = 0.0;
paraStyle.paragraphSpacingBefore = 0.0;
paraStyle.headIndent = 0;
paraStyle.tailIndent = 0;
NSDictionary *dic =@{NSFontAttributeName:font,NSParagraphStyleAttributeName:paraStyle,NSKernAttributeName:@1.0f
};
CGSize size = [string boundingRectWithSize:CGSizeMake(width,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return size.height;
}
- 算出 Label 高度
网友评论