美文网首页
YYLabel富文本

YYLabel富文本

作者: 人仙儿a | 来源:发表于2017-09-22 15:02 被阅读592次

之前一直没用YYLabel做过富文本,感觉out了,前两年面试的时候就有人问我,可是我很偏执,或者说对RichEditor偏执,觉得它是最强大的,别的都不行。但是RichEditor的文档很少,用的人相对来说也少,而且写起来很复杂,一点也不友好,会有很多坑,只是因为我第一家公司用的RichEditor,而我会大概的用法,导致了我的不敢用新的东西。其实现在业界很热衷YYLabel,用起来相对来说简单,作者也是个中国人,总得来说YYLabel,友好,易用,简单。

写了一个例子,一段文字首尾各插入一张图片:

_label = [[YYLabel alloc] initWithFrame:CGRectMake(100, 300, 250, 150)];
    _label.numberOfLines = 0;
    _label.backgroundColor = [UIColor yellowColor];
//    _label.preferredMaxLayoutWidth = 250;//我觉得不写也可以
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"你不错哦 还可以哦我上天是龙下山是上天是龙下山是上天是龙下山是上天是龙下山是虎我是人间堂堂的大英雄我还可以的over"];
    YYAnimatedImageView *imgView1 = [[YYAnimatedImageView alloc] initWithImage:[UIImage imageNamed:@"a"]];
    imgView1.frame = CGRectMake(0, 0, 20, 20);
    
    YYAnimatedImageView *imgView2 = [[YYAnimatedImageView alloc] initWithImage:[UIImage imageNamed:@"b"]];
    imgView1.frame = CGRectMake(0, 0, 20, 20);
    
    NSMutableAttributedString *attchText1 = [NSMutableAttributedString attachmentStringWithContent:imgView1 contentMode:UIViewContentModeScaleAspectFit attachmentSize:imgView1.frame.size alignToFont:[UIFont systemFontOfSize:20] alignment:YYTextVerticalAlignmentCenter];
    
    NSMutableAttributedString *attchText2 = [NSMutableAttributedString attachmentStringWithContent:imgView2 contentMode:UIViewContentModeScaleAspectFit attachmentSize:imgView2.frame.size alignToFont:[UIFont systemFontOfSize:20] alignment:YYTextVerticalAlignmentCenter];
    
    [attr insertAttributedString:attchText1 atIndex:0];
    [attr appendAttributedString:attchText2];
    
    _label.attributedText = attr;
    
    CGSize maxSize = CGSizeMake(250, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:attr];
    _label.textLayout = layout;
    CGFloat introHeight = layout.textBoundingSize.height;
    _label.width = 250;
    _label.height = introHeight;
    
    [self.view addSubview:_label];

效果图:


YYLabel

黄色部分就是YYLabel. 首尾各插入了一张图片,最后让Label计算合适的高度,效果很好!

相关文章

  • YYLabel富文本

    之前一直没用YYLabel做过富文本,感觉out了,前两年面试的时候就有人问我,可是我很偏执,或者说对RichEd...

  • YYLabel富文本

    YYLabel与UILabel对表情的支持YYLabel只支持YYTextAttachment,不支持系统默认的N...

  • YYKit框架学习之YYLabel

    一、YYLabel 强大的富文本显示功能,可根据文字的range随意添加点击事件 1 自动换行 YYLabel *...

  • YYLabel高亮点击事件与tap手势冲突解决

    在手势代理方中,判断YYLabel富文本中是否有高亮对象

  • YYLabel富文本显示

    YYLabel实现方式 //1.简单显示label YYLabel *label = [YYLabel new];...

  • YYText源码分析

    YYText 简单介绍 YYText 是YYKit中的一个富文本显示,编辑组件,拥有YYLabel,YYText...

  • YYLabel的富文本设置

    实现以上红框内的效果,设置部分字体内容的颜色为红色,前面按钮可以点击,红色字体内容也可以点击: 1、导入YYTex...

  • YY系列---YYLabel、YYText初步使用

    第一天 YYlabel使用场景:用于富文本局部文本响应点击(至少我们公司如此),具体举例,如社交类型的app,点击...

  • iOS富文本YYLabel_XM继承自YYLabel

    项目中遇到富文本的展示和点击,找了很久,发现都没封装,功能虽然强大,学习成本大,用起来太复杂!!!,所以自己封装了...

  • 计算YYLabel内容富文本的高度

    计算YYLabel内容富文本(带行间距)attributeStr的高度,假设行间距间距是5,走的弯路: 一、根据内...

网友评论

      本文标题:YYLabel富文本

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