一、YYLabel

强大的富文本显示功能,可根据文字的range随意添加点击事件




1 自动换行

    YYLabel  *yyLabel = [YYLabel new];
    yyLabel.numberOfLines = 0;

//创建容器
    YYTextContainer *titleContarer = [YYTextContainer new];
    //限制宽度
    titleContarer.size = CGSizeMake([UIScreen mainScreen].bounds.size.width-40,CGFLOAT_MAX);
    //设置富文本
    NSMutableAttributedString  *resultAttr = [self getAttr:title];
    //根据容器和文本创建布局对象
    YYTextLayout *titleLayout = [YYTextLayout layoutWithContainer:titleContarer text:resultAttr];
    //得到文本高度
    CGFloat titleLabelHeight = titleLayout.textBoundingSize.height;
    //设置frame
    yyLabel.frame = CGRectMake(20,84,[UIScreen mainScreen].bounds.size.width-40,titleLabelHeight);

2 各种格式设置

//对齐方式 这里是 两边对齐
    resultAttr.alignment = NSTextAlignmentCenter;
    //设置行间距
    resultAttr.lineSpacing = 3;
    resultAttr.font = [UIFont systemFontOfSize:20];
    {
        NSRange range =  [attributedString rangeOfString:@"it was the worst of times"];
        [resultAttr setFont:[UIFont boldSystemFontOfSize:30] range:range];
    }
    
//描边
    {
        NSRange range =[attributedString rangeOfString:@"it was the age of wisdom"];
        //文字描边(空心字)默认黑色,必须设置width
        [resultAttr setStrokeColor:[UIColor orangeColor] range:range];
        [resultAttr setStrokeWidth:@(2) range:range];
    }

//划线
    {
        NSRange range = [attributedString rangeOfString:@"it was the age of foolishness, it was the season of light" options:NSCaseInsensitiveSearch];
        YYTextDecoration *decoration = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle
                                                                       width:@(1)
                                                                       color:[UIColor blueColor]];
        //删除样式
        [resultAttr setTextStrikethrough:decoration range:range];
        //下划线
        [resultAttr setTextUnderline:decoration range:range];
    }
    
//设置边框
    {
        NSRange range = [attributedString rangeOfString:@"这是最好的时代,这是最坏的时代" options:NSCaseInsensitiveSearch];
        //边框
        YYTextBorder *border = [YYTextBorder new];
        border.strokeColor = [UIColor redColor];
        border.strokeWidth = 4;
        border.lineStyle = YYTextLineStylePatternDashDotDot;
        border.cornerRadius = 1;
        border.insets = UIEdgeInsetsMake(0, -2, 0, -2);
        [resultAttr setTextBorder:border range:range];

}
    
//设置阴影
    {
        NSRange range = [attributedString rangeOfString:@"这是智慧的时代,这是愚蠢的时代" options:NSCaseInsensitiveSearch];
        //阴影
        NSShadow *shadow = [[NSShadow alloc] init];
        [shadow setShadowColor:[UIColor redColor]];
        [shadow setShadowBlurRadius:1.0];
        [shadow setShadowOffset:CGSizeMake(2, 2)];
        [resultAttr setShadow:shadow range:range];
    }

3 高亮显示文本 点击交互事件

{
        NSRange range = [attributedString rangeOfString:@"这是希望之春,这是失望之冬" options:NSCaseInsensitiveSearch];
        YYTextBorder *border = [YYTextBorder new];
        border.cornerRadius = 50;
        border.insets = UIEdgeInsetsMake(0, -10, 0, -10);
        border.strokeWidth = 0.5;
        border.strokeColor = [UIColor yellowColor];
        border.lineStyle = YYTextLineStyleSingle;
        [resultAttr setTextBorder:border range:range];
        [resultAttr setTextBackgroundBorder:border range:range];
        [resultAttr setColor:[UIColor greenColor] range:range];

YYTextBorder *highlightBorder = border.copy;
        highlightBorder.strokeWidth = 0;
        highlightBorder.strokeColor =  [UIColor purpleColor];
        highlightBorder.fillColor =  [UIColor purpleColor];
        
        YYTextHighlight *highlight = [YYTextHighlight new];
        [highlight setColor:[UIColor whiteColor]];
        [highlight setBackgroundBorder:highlightBorder ];
        highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
            [self alertShow:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
        };
        [resultAttr setTextHighlight:highlight range:range];
        
        
        // 点击复制
        [resultAttr setTextHighlightRange:[attributedString rangeOfString:@"450351763"]
                                    color:[UIColor greenColor]
                          backgroundColor:[UIColor whiteColor]
                                tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
                                    UIPasteboard *pboard = [UIPasteboard generalPasteboard];
                                    pboard.string = @"450351763";
                                    [self alertShow:@"复制成功"];
                            
                                }];
    }

4 图文混排 支持各种格式包括gif

{
        for (int i = 1; i<5; i++) {
            NSString *path;
            if(i == 4){
                path = [[NSBundle mainBundle] pathForScaledResource:[NSString stringWithFormat:@"%d",i] ofType:@"gif"];
            }else{
                path = [[NSBundle mainBundle] pathForScaledResource:[NSString stringWithFormat:@"%d",i] ofType:@"jpg"];
            }
            NSData *data = [NSData dataWithContentsOfFile:path];
            //修改表情大小
            YYImage *image = [YYImage imageWithData:data scale:3];
            image.preloadAllAnimatedImageFrames = YES;
            YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];
            NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:[UIFont systemFontOfSize:18] alignment:YYTextVerticalAlignmentCenter];
            [resultAttr appendAttributedString:attachText];
        }
    }

YYkit推荐pod安装方式(我使用的是  pod 'YYKit', '~> 1.0.9' 最新版本)

以下为全部代码贴到项目中可以直接运行查看效果

#import <YYKit/YYKit.h>@interface ViewController2 ()@end
@implementation ViewController2- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor colorWithRed:0.3 green:0.5 blue:0.8 alpha:1];[self showYYLabel];
}
-(void)showYYLabel{NSString *title = @"It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the season of light, it was the season of darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us. We were all going direct to heaven, we were all going direct the other way.\n这是最好的时代,这是最坏的时代;这是智慧的时代,这是愚蠢的时代;这是信仰的时期,这是怀疑的时期;这是光明的季节,这是黑暗的季节;这是希望之春,这是失望之冬;人们面前有着各样事物,人们面前一无所有;人们正在直登天堂,人们正在直下地狱。\n点击复制QQ450351763进行在线咨询";YYLabel  *yyLabel = [YYLabel new];yyLabel.backgroundColor = [UIColor colorWithRed:0.3 green:0.4 blue:0.1 alpha:1];//异步显示yyLabel.displaysAsynchronously = YES;yyLabel.numberOfLines = 0;//创建容器YYTextContainer *titleContarer = [YYTextContainer new];//限制宽度titleContarer.size = CGSizeMake([UIScreen mainScreen].bounds.size.width-40,CGFLOAT_MAX);//设置富文本NSMutableAttributedString  *resultAttr = [self getAttr:title];//根据容器和文本创建布局对象YYTextLayout *titleLayout = [YYTextLayout layoutWithContainer:titleContarer text:resultAttr];//得到文本高度CGFloat titleLabelHeight = titleLayout.textBoundingSize.height;//设置frameyyLabel.frame = CGRectMake(20,84,[UIScreen mainScreen].bounds.size.width-40,titleLabelHeight);yyLabel.attributedText = titleAttr;[self.view addSubview:yyLabel];
}
- (NSMutableAttributedString*)getAttr:(NSString*)attributedString {NSMutableAttributedString *resultAttr = [[NSMutableAttributedString alloc] initWithString:attributedString];//    一、 格式设置//对齐方式 这里是 两边对齐resultAttr.alignment = NSTextAlignmentCenter;//设置行间距resultAttr.lineSpacing = 3;resultAttr.font = [UIFont systemFontOfSize:20];{NSRange range =  [attributedString rangeOfString:@"it was the worst of times"];[resultAttr setFont:[UIFont boldSystemFontOfSize:30] range:range];}//描边{NSRange range =[attributedString rangeOfString:@"it was the age of wisdom"];//文字描边(空心字)默认黑色,必须设置width[resultAttr setStrokeColor:[UIColor orangeColor] range:range];[resultAttr setStrokeWidth:@(2) range:range];}//划线{NSRange range = [attributedString rangeOfString:@"it was the age of foolishness, it was the season of light" options:NSCaseInsensitiveSearch];YYTextDecoration *decoration = [YYTextDecoration decorationWithStyle:YYTextLineStyleSinglewidth:@(1)color:[UIColor blueColor]];//删除样式[resultAttr setTextStrikethrough:decoration range:range];//下划线[resultAttr setTextUnderline:decoration range:range];}//设置边框{NSRange range = [attributedString rangeOfString:@"这是最好的时代,这是最坏的时代" options:NSCaseInsensitiveSearch];//边框YYTextBorder *border = [YYTextBorder new];border.strokeColor = [UIColor redColor];border.strokeWidth = 4;border.lineStyle = YYTextLineStylePatternDashDotDot;border.cornerRadius = 1;border.insets = UIEdgeInsetsMake(0, -2, 0, -2);[resultAttr setTextBorder:border range:range];}//设置阴影{NSRange range = [attributedString rangeOfString:@"这是智慧的时代,这是愚蠢的时代" options:NSCaseInsensitiveSearch];//阴影NSShadow *shadow = [[NSShadow alloc] init];[shadow setShadowColor:[UIColor redColor]];[shadow setShadowBlurRadius:1.0];[shadow setShadowOffset:CGSizeMake(2, 2)];[resultAttr setShadow:shadow range:range];}//高亮显示文本{NSRange range = [attributedString rangeOfString:@"这是希望之春,这是失望之冬" options:NSCaseInsensitiveSearch];YYTextBorder *border = [YYTextBorder new];border.cornerRadius = 50;border.insets = UIEdgeInsetsMake(0, -10, 0, -10);border.strokeWidth = 0.5;border.strokeColor = [UIColor yellowColor];border.lineStyle = YYTextLineStyleSingle;[resultAttr setTextBorder:border range:range];[resultAttr setTextBackgroundBorder:border range:range];[resultAttr setColor:[UIColor greenColor] range:range];YYTextBorder *highlightBorder = border.copy;highlightBorder.strokeWidth = 0;highlightBorder.strokeColor =  [UIColor purpleColor];highlightBorder.fillColor =  [UIColor purpleColor];YYTextHighlight *highlight = [YYTextHighlight new];[highlight setColor:[UIColor whiteColor]];[highlight setBackgroundBorder:highlightBorder ];highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {[self alertShow:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];};[resultAttr setTextHighlight:highlight range:range];// 点击复制[resultAttr setTextHighlightRange:[attributedString rangeOfString:@"450351763"]color:[UIColor greenColor]backgroundColor:[UIColor whiteColor]tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){UIPasteboard *pboard = [UIPasteboard generalPasteboard];pboard.string = @"450351763";[self alertShow:@"复制成功"];}];}//    图文混排{for (int i = 1; i<5; i++) {NSString *path;if(i == 4){path = [[NSBundle mainBundle] pathForScaledResource:[NSString stringWithFormat:@"%d",i] ofType:@"gif"];}else{path = [[NSBundle mainBundle] pathForScaledResource:[NSString stringWithFormat:@"%d",i] ofType:@"jpg"];}NSData *data = [NSData dataWithContentsOfFile:path];//修改表情大小YYImage *image = [YYImage imageWithData:data scale:3];image.preloadAllAnimatedImageFrames = YES;YYAnimatedImageView *imageView = [[YYAnimatedImageView alloc] initWithImage:image];NSMutableAttributedString *attachText = [NSMutableAttributedString attachmentStringWithContent:imageView contentMode:UIViewContentModeCenter attachmentSize:imageView.size alignToFont:[UIFont systemFontOfSize:18] alignment:YYTextVerticalAlignmentCenter];[resultAttr appendAttributedString:attachText];}}return resultAttr;
}-(void)alertShow:(NSString *)str{UIAlertController *vc =  [UIAlertController alertControllerWithTitle:nil message:str preferredStyle:UIAlertControllerStyleActionSheet];[self.navigationController presentViewController:vc animated:YES completion:^{dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{[vc dismissViewControllerAnimated:YES completion:^{}];});}];
}

以后会陆续更新YYkit其他框架

YYKit 常见用法总结相关推荐

  1. mysql中正则表达式的用法_Mysql中正则表达式Regexp常见用法

    Mysql中Regexp常见用法 模糊匹配,包含特定字符串 # 查找content字段中包含"车友俱乐部"的记录 select * from club_content where ...

  2. python六:常见数据类型以及常见用法

    bool 布尔值 # 在bool中 0,()(空元祖),[](空列表),{}(空字典),None,""(空字符串) 都是False int 整形 float 浮点型 str 字符串 ...

  3. vue-router 的常见用法

    vue-router 的常见用法 1. 路由重定向 路由重定向指的是:用户在访问地址 A 的时候,强制用户跳转到地址 C ,从而展示特定的组件页面. 通过路由规则的 redirect 属性,指定一个新 ...

  4. 空的宏定义作用及常见用法

    空的宏定义作用及常见用法 我们学习宏定义的第一个概念是,宏定义作为替换作用. 例如: #define SIZE 32 // 接下来代码使用SIZE就相当于32 宏定义是编译器在编译时,将程序中所有SI ...

  5. Arrays常见用法

    Arrays常见用法

  6. sed文本处理常见用法

    sed文本处理常见用法    2011-06-24  TsengYia#126.com http://tsengyia.blog.chinaunix.net/   ################## ...

  7. typeset的常见用法

    [保留] typeset的常见用法 http://www.chinaunix.net 作者:寂寞烈火  发表于:2007-12-16 09:50:34 [发表评论] [查看原文] [Shell讨论区] ...

  8. 框架前期准备篇之AutoFac常见用法总结 转载

    框架前期准备篇之AutoFac常见用法总结 一. 说在前面的话 凡是大约工作在两年以上的朋友们,或多或少都会接触到一些框架搭建方面的知识,只要一谈到框架搭建这个问题或者最佳用法这个问题,势必会引起一点 ...

  9. Java集合Collection与List的关系、常见用法

    关系树 [java] view plain copy print? ---|Collection: 单列集合 ---|List: 有存储顺序, 可重复 ---|ArrayList: 数组实现, 查找快 ...

  10. sscanf的常见用法

    例子:  1. 常见用法.  char buf[512] = ;  sscanf("123456 ", "%s", buf);  printf("%s ...

最新文章

  1. C#读取Win32标准DLL文件中的字符串资源
  2. 第二十二部分_Hibernate检索策略、拦截器、事务隔离级别
  3. 23种设计模式C++源码与UML实现--组合模式
  4. 云原生系列「五」我为啥又看上了serviceMesh?
  5. “互联网+”解决城市交通拥堵难题
  6. GalleryView
  7. Elasticsearch-kopf导览
  8. [Unity3D]无缝场景切换解决方案(1) - 简单场景切换
  9. Unity TUIO雷达入门
  10. Windows 强制删除文件及文件夹命令
  11. tcp/udp 常用端口列表
  12. 在Edge浏览器中设置编码方式
  13. ODT,ZQ校准,OCT,TDQS
  14. 简单聊聊MD5和SHA-256加密
  15. CSS 实现优惠券的技巧
  16. 量化策略——准备2 量化技能树量化术语
  17. 无兄弟,不编程!在兄弟连我学到的不仅仅是PHP
  18. Uml 理解Rational Rose软件中四种视图和Uml 9类图之间的关系
  19. 非递归遍历二叉树 Java
  20. 嵌入式linux之Nor/Nand FLASH的读写

热门文章

  1. 计算机辅助机械设计实训教程,机械设计教学中计算机辅助设计应用
  2. 【zotero】异常与错误,Attachments skipped because they are top-level items,snapshots,an avoided filetype解决方法
  3. 【以前的空间】网络流合集
  4. 高级售前客户服务专员题库
  5. [网络应用]11款来自 deviantART 的 foobar 2000 皮肤
  6. 大智慧c语言dll,调用大智慧dll,简单支持大智慧公式dll接口
  7. SQL Server2005+SQL Server2000下载
  8. 32位java jre_jre-7u4-windows-i586.exe|java 1.7.0(Java TM 7)JRE7 32位_最火软件站
  9. Rainmeter雨滴桌面秀教程
  10. IDEA快速生成测试用例类和完成单元测试