YYKit 学习笔记之 YYLabel

本文是关于YYKit的学习笔记,重点介绍了如何通过Cocoapods安装YYKit,并详细讲解了YYLabel的使用。安装步骤包括修改Podfile,添加'YYKit', '~> 1.0.9',然后执行pod install。YYKit包含多个组件,如YYModel、YYCache、YYImage等,但本文主要关注YYText框架中的YYLabel,它是一个功能强大的富文本框架。" 78992428,6943737,理解与模拟实现memcpy函数,"['C语言', '函数实现', '内存管理']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


YYKit 学习笔记之 YYLabel


使用Cocoapods安装YYKit
YYKit GitHub传送门:https://github.com/ibireme/YYKit
一.打开工程的 Podfile文件
二.在Podfile文件中添加pod ‘YYKit’, ‘~> 1.0.9’
三.终端进入工程目录,执行pod install

YYKit 添加成功

添加头文件
#import <YYKit/YYKit.h>

NSString *textStr = @"The YYLabel class implements a read-only text view,这是删除样式~~这是下划线样式~~这是带边框样式,这是带阴影样式,点击交互事件,添加点击事件,分割分割分割";
    YYLabel * label = [[YYLabel alloc] initWithFrame:CGRectZero];
    label.backgroundColor = [UIColor grayColor];
    label.numberOfLines = 0;
    label.textVerticalAlignment =  YYTextVerticalAlignmentTop;//垂直属性,上  下 或居中显示

 //富文本属性
    NSMutableAttributedString  * attriStr = [[NSMutableAttributedString alloc] initWithString:textStr];

 //设置行间距
    attriStr.lineSpacing = 10;
    attriStr.font = [UIFont systemFontOfSize:20];
   
    //富文本属性 
 NSRange range =[textStr rangeOfString:@"The YYLabel class implements a read-only text view"];
    [attriStr setFont:[UIFont boldSystemFontOfSize:30] range:range];

 //删除样式
    NSRange range2 =[textStr rangeOfString:@"这是删除样式" options:NSCaseInsensitiveSearch];
    YYTextDecoration *deletDecoration = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle width:@(1) color:[UIColor redColor]];
    [attriStr setTextStrikethrough:deletDecoration range:range2];




//下划线
    NSRange range3 =[textStr rangeOfString:@"这是下划线样式" options:NSCaseInsensitiveSearch];
    YYTextDecoration *decoration = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle width:@(2) color:[UIColor yellowColor]];
    [attriStr setTextUnderline:decoration range:range3];



 //文本设置边框
    NSRange range4 = [textStr rangeOfString:@"这是带边框样式" options:NSCaseInsensitiveSearch];
    //边框
    YYTextBorder *border = [YYTextBorder new];
    border.strokeColor = [UIColor greenColor];
    border.strokeWidth = 1;
    border.lineStyle = YYTextLineStyleSingle;
    border.cornerRadius = 1;
    border.insets = UIEdgeInsetsMake(0, -2, 0, -2);
    [attriStr setTextBorder:border range:range4];


  //设置阴影
    NSRange range5 = [textStr rangeOfString:@"这是带阴影样式" options:NSCaseInsensitiveSearch];
    //阴影
    NSShadow *shadow = [[NSShadow alloc] init];
    [shadow setShadowColor:[UIColor redColor]];
    [shadow setShadowBlurRadius:1.0];
    [shadow setShadowOffset:CGSizeMake(2, 2)];
    [attriStr setShadow:shadow range:range5];


 //高亮显示文本 点击交互事件
    NSRange range6 =[textStr rangeOfString:@"点击交互事件" options:NSCaseInsensitiveSearch];
    YYTextBorder *border2 = [YYTextBorder new];
    border2.cornerRadius = 50;
    border2.insets = UIEdgeInsetsMake(0, -10, 0, -10);
    border2.strokeWidth = 0.5;
    border2.strokeColor = [UIColor yellowColor];
    border2.lineStyle = YYTextLineStyleSingle;
    [attriStr setTextBorder:border2 range:range6];
    [attriStr setColor:[UIColor greenColor] range:range6];
    
    YYTextBorder *highlightBorder = border2.copy;
    highlightBorder.strokeWidth = 1;
    highlightBorder.strokeColor =  [UIColor purpleColor];
    highlightBorder.fillColor =  [UIColor purpleColor];
    YYTextHighlight *highlight = [YYTextHighlight new];
    [highlight setColor:[UIColor orangeColor]];
    [highlight setBackgroundBorder:highlightBorder ];
    highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        NSLog(@"点击事件");
        [self back];
        
    };
   [attriStr setTextHighlight:highlight range:range6];




  NSRange range7 =[textStr rangeOfString:@"添加点击事件" options:NSCaseInsensitiveSearch];
    YYTextHighlight *highlight2 = [YYTextHighlight new];
    [highlight2 setColor:[UIColor orangeColor]];//点击时字体颜色
    highlight2.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        NSLog(@"点击事件222");
        [self back];
        
    };
    [attriStr setTextHighlight:highlight2 range:range7];



 NSRange range8 =[textStr rangeOfString:@"分割分割分割"];
    [attriStr setFont:[UIFont boldSystemFontOfSize:25] range:range8];
    [attriStr setColor:[UIColor redColor] range:range8];


 // 图文混排 支持各种格式包括gif
   YYImage *image = [YYImage imageNamed:@"house1"];
   image.preloadAllAnimatedImageFrames = YES;
   YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
   NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeTop attachmentSize:imageView.size alignToFont:[UIFont systemFontOfSize:18] alignment:YYTextVerticalAlignmentCenter];
   // [attriStr appendAttributedString:attachText];
    [attriStr insertAttributedString:attachText atIndex:range8.location];
    
    
    //Gif图片
   NSString *  path = [[NSBundle mainBundle] pathForScaledResource:@"gif_loading" ofType:@"gif"];
   NSData *data = [NSData dataWithContentsOfFile:path];
    //修改表情大小
    YYImage *image2 = [YYImage imageWithData:data scale:2];
    image2.preloadAllAnimatedImageFrames = YES;
    YYAnimatedImageView *imageView2 = [[YYAnimatedImageView alloc] initWithImage:image2];
    NSMutableAttributedString *attachText2 = [NSMutableAttributedString attachmentStringWithContent:imageView2 contentMode:UIViewContentModeCenter attachmentSize:imageView2.size alignToFont:[UIFont systemFontOfSize:18] alignment:YYTextVerticalAlignmentCenter];
    [attriStr appendAttributedString:attachText2];

    

    
    label.attributedText = attriStr;
  // 创建容器
    YYTextContainer *container = [[YYTextContainer alloc] init];
    //限制宽度
    container.size = CGSizeMake(300, CGFLOAT_MAX);
    
    //根据容器和文本创建布局对象
    YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:attriStr];
    //得到文本高度
    CGFloat titleLabelHeight = layout.textBoundingSize.height;
    //设置frame
    label.frame = CGRectMake(20,84,300,titleLabelHeight);
    //此处的YYTextLayout可以计算富文本的高度,他有一个属性textBoundingRect和textBoundingSize,container.size是用来限制宽度的,可以计算高度
    //YYTextLayout是用来赋值给YYLabel,相当于UILabel的attributedText
    //如果单纯的做点击处理可以用attributedText直接赋值给YYLabel,但是如果需要异步渲染就必须用YYTextLayout
    [self.view addSubview:label];


    
    

效果图

YYModel — 高性能的 iOS JSON 模型框架
YYCache — 高性能的 iOS 缓存框架。
YYImage — 功能强大的 iOS 图像框架。
YYWebImage — 高性能的 iOS 异步图像加载框架。
YYText — 功能强大的 iOS 富文本框架。
YYKeyboardManager — iOS 键盘监听管理工具。
YYDispatchQueuePool — iOS 全局并发队列管理工具。
YYAsyncLayer — iOS 异步绘制与显示的工具。
YYCategories — 功能丰富的 Category 类型工具库。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值