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]

源码WPAttributedMarkup, WPAttributedMarkup能给Label中某关键字添加文字效果或点击事件。Label中的某个关键字词可以改变字体的特性如颜色、加粗,下划线等,也可以为某个关键字词添加点击事件。

<ignore_js_op>

使用方法:

通过创建不同的style的字典后就可以对label的属性进行设置,示例如下:

// Example using fonts and colours

NSDictionary* style1 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:18.0],

@"bold":[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],

@"red": [UIColor redColor]}

// Example using arrays of styles, dictionary attributes for underlining and image styles

NSDictionary* style2 = @{@"body" :

@[[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],

[UIColor darkGrayColor]],

@"u": @[[UIColor blueColor],

@{NSUnderlineStyleAttributeName : @(kCTUnderlineStyleSingle|kCTUnderlinePatternSolid)}

],

@"thumb":[UIImage imageNamed:@"thumbIcon"] }

// Example using blocks for actions when text is tapped. Uses the 'link' attribute to style the links

NSDictionary* style3 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:22.0],

@"help":[WPAttributedStyleAction styledActionWithAction:^{

NSLog(@"Help action")

}],

@"settings":[WPAttributedStyleAction styledActionWithAction:^{

NSLog(@"Settings action")

}],

@"link": [UIColor orangeColor]}

self.label1.attributedText = [@"AttributedBoldRedtext" attributedStringWithStyleBook:style1]

self.label2.attributedText = [@"[td]Multiplestylestext[td]" attributedStringWithStyleBook:style2]

<br/>

本文来源于<b>大学IT网</b>

 


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存