美文网首页
YYKit 学习笔记之 YYLabel

YYKit 学习笔记之 YYLabel

作者: hanyongwei | 来源:发表于2021-03-02 09:42 被阅读0次
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];

2019061814054121.png

原文链接:https://blog.csdn.net/xu_yu_jie/article/details/92784800

相关文章

网友评论

      本文标题:YYKit 学习笔记之 YYLabel

      本文链接:https://www.haomeiwen.com/subject/ekdtqltx.html