YYLabel富文本显示

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];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容