YYLabel富文本显示

YYLabel富文本显示,第1张

YYLabel实现方式

//1.简单显示label

YYLabel *label = [YYLabel new]

label.frame = CGRectMake(100,50,100,25)

label.font = [UIFont systemFontOfSize:14.0f]

label.textColor = [UIColororangeColor]

label.textAlignment=NSTextAlignmentCenter

label.lineBreakMode=0

label.numberOfLines = NSLineBreakByWordWrapping

label.text=@"YYTextDemo Test"

//    [self.view addSubview:label]

//2.属性字符串 简单实用

NSMutableAttributedString*text= [[NSMutableAttributedStringalloc] initWithString:@"上海市第九城市信息技术有限公司"]

text.font = [UIFontboldSystemFontOfSize:13.0f]

text.color = [UIColorblueColor]

[textsetColor:[UIColor redColor]range:NSMakeRange(0,4)]

//    给你的label添加点击事件

[textsetTextHighlightRange:NSMakeRange(0,4)

color:[UIColor orangeColor]

backgroundColor:[UIColorwhiteColor]

tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){

NSLog(@"tap text range:...")

}]

YYLabel *attributedLabel = [YYLabel new]

attributedLabel.frame = CGRectMake(100,100,160,25)

attributedLabel.attributedText = text

attributedLabel.userInteractionEnabled=YES

attributedLabel.backgroundColor = [UIColor purpleColor]

//    [self.view addSubview:attributedLabel]

//3.图文混排模式

NSMutableAttributedString *textImage = [NSMutableAttributedString new]

UIFont *font= [UIFont systemFontOfSize:14.0f]

inti =2

switch(i) {

case0:

{

//    方式一

NSString*title =@"2006年的诺贝尔文学奖颁给了土耳其作家奥尔罕.帕慕克。在很多人都很意外的时候,我心头却感到一阵开心,因为早在两年前,我就知道了这个作家"

[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]]

UIImage *image = [UIImage imageNamed:@"babilogo"]

image= [UIImage imageWithCGImage:image.CGImage scale:5orientation:UIImageOrientationUp]

NSMutableAttributedString*attachText= [NSMutableAttributedStringattachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]

[textImage appendAttributedString:attachText]

[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:@"凭借《我的名字叫红》获得了都柏林文学..."attributes:nil]]

break

}

case1:

{

NSString *title=@"text 与 UIView混排:"

[textImageappendAttributedString:[[NSAttributedStringalloc] initWithString:title attributes:nil]]

UISwitch *switcher = [UISwitch new]

switcher.frame= CGRectMake(0,0,50,50)

[switcher sizeToFit]

NSMutableAttributedString*attachText = [NSMutableAttributedString attachmentStringWithContent:switcher contentMode:UIViewContentModeCenter attachmentSize:switcher.frame.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]

[textImageappendAttributedString:attachText]

[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"attributes:nil]]

break

}

case2:

{

NSString*title =@"2006年的诺贝尔文学奖颁给了土耳其作家奥尔罕.帕慕克。在很多人都很意外的时候,我心头却感到一阵开心,因为早在两年前,我就知道了这个作家"

[textImage appendAttributedString:[[NSAttributedString alloc] initWithString:title attributes:nil]]

UIImageView *imageView = [UIImageView new]

imageView.frame= CGRectMake(0,0,20,20)

[imageViewsetImage:[UIImage imageNamed:@"babilogo"]]

imageView.contentMode =UIViewContentModeScaleAspectFill

imageView.userInteractionEnabled=YES

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id_Nonnullsender) {

NSLog(@"ImageAction")

}]

[imageViewaddGestureRecognizer:tap]

NSMutableAttributedString*attachText = [NSMutableAttributedStringattachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.frame.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]

[textImageappendAttributedString:attachText]

NSString*subTitle=@"凭借《我的名字叫红》获得了都柏林文学,2006年的诺贝尔文学奖颁给了土耳其作家奥尔罕.帕慕克。在很多人都很意外的时候,我心头却感到一阵开心,因为早在两年前,我就知道了这个作家"

[textImageappendAttributedString:[[NSAttributedString alloc] initWithString:subTitle attributes:nil]]

NSMutableParagraphStyle*paragraphStyle= [[NSMutableParagraphStylealloc] init]

[paragraphStylesetLineSpacing:16]//调整行间距

[textImage addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [title length]+[subTitle length])]

[textImage setTextHighlightRange:NSMakeRange(0,8)

color:[UIColor orangeColor]

backgroundColor:[UIColor whiteColor]

tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){

NSLog(@"tap text range:...")

}]

break

}

default:

break

}

textImage.font = font

YYLabel *textImageLabel = [YYLabel new]

textImageLabel.userInteractionEnabled =YES

textImageLabel.numberOfLines =0

textImageLabel.frame = CGRectMake(0,0,320,400)

textImageLabel.attributedText = textImage

//    [self.view addSubview:textImageLabel]

基本实现思路就是利用正则识别链接加YYLabel设置链接点击事件,下面说下这过程中遇到的问题

1、自动布局YYLabel后发现设置label.numberOfLines = 0后不换行,YYLabel还得设置一个preferredMaxLayoutWidth属性,这个属性是设置最大宽度,设置完才能有换行功能

2、添加YYLabel的分类并且利用正则识别链接

3、通过给NSAttributedString设置高亮并且添加点击事件

4、添加完点击事件后点击发现并没有响应,是因为label的父类试图添加了点击手势导致的手势冲突解决办法是给父试图的手势添加代理并且实现代理方法


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11407513.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存