最近,因为这个Lable多个字段点击事件头疼不已,一开始用的别的第三方,最终总是Cell高度计算不准确,纠结了好久,后来尝试用YYKit框架来试试看,果然,功夫不负有心人,终于解决了Cell高度不准确,Label多处点击事件的问题。
首先肯定是要 导入YYKit框架咯。
直接看代码吧:
-(void)tet2{
NSDictionary *dic1 = @{@"type":@"0",@"content":@"您好"};
NSDictionary *dic2 = @{@"type":@"2",@"content":@"订单加好友@你"};
NSDictionary *dic5 = @{@"type":@"0",@"content":@"。点击查看可"};
NSDictionary *dic6 = @{@"type":@"6",@"content":@"添加好友!"};
NSMutableArray * array2 = [[NSMutableArray alloc]initWithObjects:dic1,dic2,dic5,dic6 ,nil];
NSMutableArray *strArr2 =[NSMutableArray arrayWithCapacity:0];
for (NSDictionary *dict in array2) {
[strArr2 addObject:dict[@"content"]];
}
_pinChuan2 = [strArr2 componentsJoinedByString:@""];
// 1. Create an attributed string.
NSMutableAttributedString *text2 = [[NSMutableAttributedString alloc] initWithString:_pinChuan2];
// 2. Set attributes to text, you can use almost all CoreText attributes.
text2.font = [UIFont boldSystemFontOfSize:16.0f];
text2.color = [UIColor blueColor];
text2.lineSpacing = 0;
__weak typeof(self) weakSelf = self;
for (NSDictionary * dict2 in array2 ) {
// NSLog(@"dic----%@",dic2);
if ([dict2[@"type"]integerValue] != 0) {
NSString * content2 = dict2[@"content"];
NSRange range2 = [_pinChuan2 rangeOfString:content2];
[text2 setTextHighlightRange:range2 color:[UIColor orangeColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
[weakSelf didSelectedDic:dict2];
}];
}
}
// 3. Set to YYLabel or YYTextView.
YYLabel *label2 = [YYLabel new];
CGFloat Height2 = [self getterSpaceLabelHeight:_pinChuan2 withLineSpacing:0 withFont:[UIFont systemFontOfSize:16.0f] withWidth:KScreenWidth - 40];
NSLog(@"string2---->%@ height2---> %f",_pinChuan2,Height2);
label2.frame = CGRectMake(20, 200,KScreenWidth - 40, Height2);
label2.attributedText = text2;
label2.numberOfLines = 1;
label2.font = [UIFont systemFontOfSize:16.0f];
label2.backgroundColor = [UIColor lightGrayColor];
[label2 sizeToFit];
[self.view addSubview:label2];
}
- (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;
}
-(void)didSelectedDic:(NSDictionary *)dic{
NSLog(@"type---%@ content-----%@",dic[@"type"],dic[@"content"]);
}
看看效果图
最后想说,YYKit真的很强大,没用过的同学多看看这框架吧。大神勿喷。
https://github.com/sunyafeng1990/YYLabelTest
网友评论