一般用富文本实现多种花样的Label文字,下图是利用UILabel分类快速创建的多彩多样lable文字,快速简单,自定义性强,更重要的是无代码污染,特别适合轻量级使用

https://github.com/joaoffcosta/UILabel-FormattedText

在我写这篇博客的第二天,作者就把代码全部改成swift了....

主要功能介绍:

在同一lablel中设置不同文字字体,文字颜色,下划线,删除线,字间距,斜体,图片

在类别中新加几个个方法,这样更加强大,方便使用,其它方法调用详见Demo

//  UILabel+FormattedText.h

#pragma mark--自定义添加
/*设置字体于颜色*/
- (void) setFont:(UIFont *)font substring:(NSString*)substring;
- (void) setFont:(UIFont *)font WithtColor:(UIColor *)textColor substring:(NSString*)substring; /**添加删除线 */
-(void)setTextDeleteLine:(UIColor *)lineColor range:(NSRange)range; /**斜体*/
-(void)setObliqueness:(id)obliqueness range:(NSRange)range; /** 字间距 */
-(void)setWordSpace:(id)space range:(NSRange)range; /** 添加多个属性*/
-(void)setTextAttributes:(NSDictionary *)attributes range:(NSRange)range; /*添加图片*/
-(void)addImgAttachment:(UIImage *)image size:(CGSize)size range:(NSRange)range;

  

  

//  UILabel+FormattedText.m

#pragma mark -Custom Add By Copyright (c) SIBU.Net----
- (void) setFont:(UIFont*)font substring:(NSString*)substring {
NSRange range = [self.text rangeOfString:substring];
if (range.location != NSNotFound)
{
[self setFont:font range:range];
}
} - (void) setFont:(UIFont *)font WithtColor:(UIColor *)textColor substring:(NSString*)substring
{
NSRange range = [self.text rangeOfString:substring];
if (range.location != NSNotFound)
{
[self setFont:font range:range];
[self setTextColor:textColor range:range];
}
} /**添加删除线 */
-(void)setTextDeleteLine:(UIColor *)lineColor range:(NSRange)range
{
NSMutableAttributedString *text = [self.attributedText mutableCopy];
[text addAttribute:NSStrikethroughStyleAttributeName
value:@(YES)
range:range]; [text addAttribute:NSUnderlineColorAttributeName value:lineColor range:range];
self.attributedText = text;
} /**
* 斜体,设置字体倾斜度--@0.0~@1.0
*/
-(void)setObliqueness:(id)obliqueness range:(NSRange)range{
NSMutableAttributedString *text = [self.attributedText mutableCopy];
[text addAttribute:NSObliquenessAttributeName value:obliqueness range:range];
self.attributedText = text;
} /** 字间距 */
-(void)setWordSpace:(id)space range:(NSRange)range;
{
NSMutableAttributedString *text = [self.attributedText mutableCopy];
[text addAttribute:NSKernAttributeName value:space range:range];
self.attributedText = text;
}

/** 设置多个属性*/
-(void)setTextAttributes:(NSDictionary *)attributes range:(NSRange)range
{
NSMutableAttributedString *text = [self.attributedText mutableCopy];
[text addAttributes:attributes range:range];
self.attributedText = text;
} /*添加图片*/
-(void)addImgAttachment:(UIImage *)image size:(CGSize)size range:(NSRange)range
{
NSMutableAttributedString *text = [self.attributedText mutableCopy];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [self imageWithImage:image scaleToSize:size]; NSAttributedString *iconAttributedString = [NSAttributedString attributedStringWithAttachment:textAttachment];
[text replaceCharactersInRange:range withAttributedString:iconAttributedString];
self.attributedText = text;
}

使用

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor=[UIColor blackColor]; NSString *text=@"思埠集团于2015年1月6日正式入股本土第一家在新三板上市日化企业——幸美股份(代码830929),成为幸美最大股东。广东思埠集团直接控股新三板挂牌上市公司幸美股份,正式成为准上市公司。"; UILabel *labelA = [[UILabel alloc] init];
[labelA setFrame:CGRectMake(0, 15, self.view.frame.size.width,25)];
[labelA setBackgroundColor:[UIColor clearColor]];
[labelA setTextAlignment:NSTextAlignmentCenter];
[labelA setText:@"Color Label"];
[labelA setTextColor:[UIColor cyanColor]];
[labelA setFont:[UIFont systemFontOfSize:22]];;
[labelA setTextColor:[UIColor redColor] String:@"Color"];
[self.view addSubview:labelA]; // Colors
UILabel *labelC = [[UILabel alloc] init];
[labelC setFrame:CGRectMake(0, 30, self.view.frame.size.width, floorf(self.view.frame.size.height / 5))];
[labelC setBackgroundColor:[UIColor clearColor]];
[labelC setTextAlignment:NSTextAlignmentCenter];
[labelC setText:text];
[labelC setNumberOfLines:2];
[labelC setTextColor:[UIColor yellowColor]];
[labelC setFont:[UIFont fontWithName:@"Courier" size:14]];
[labelC setTextColor:[UIColor whiteColor] range:NSMakeRange(20, 3)];
[labelC setTextColor:[UIColor cyanColor] range:NSMakeRange(28, 4)];
[labelC setTextUnderLine:[UIColor redColor] range:NSMakeRange(10, 6)];
[self.view addSubview:labelC]; // Fonts
UILabel *labelF = [[UILabel alloc] init];
[labelF setFrame:CGRectMake(0, floorf(self.view.frame.size.height / 4), self.view.frame.size.width, floorf(self.view.frame.size.height / 3))];
[labelF setBackgroundColor:[UIColor clearColor]];
[labelF setTextAlignment:NSTextAlignmentCenter];
[labelF setText:text];
[labelF setNumberOfLines:2];
[labelF setTextColor:[UIColor yellowColor]];
[labelF setFont:[UIFont fontWithName:@"Courier" size:14]];
[labelF setTextColor:[UIColor redColor] String:@"幸美股份"];
[labelF setFont:[UIFont systemFontOfSize:20] WithtColor:[UIColor whiteColor] substring:@"思埠集团"];
[self.view addSubview:labelF]; // Colors AND Fonts
UILabel *labelCF = [[UILabel alloc] init];
[labelCF setFrame:CGRectMake(0, floorf(self.view.frame.size.height * 2 / 4), self.view.frame.size.width, floorf(self.view.frame.size.height / 3))];
[labelCF setBackgroundColor:[UIColor clearColor]];
[labelCF setTextAlignment:NSTextAlignmentCenter];
[labelCF setText:text];
[labelCF setNumberOfLines:2];
[labelCF setTextColor:[UIColor yellowColor]];
[labelCF setFont:[UIFont fontWithName:@"Courier" size:14]];
[labelCF setTextColor:[UIColor redColor] range:NSMakeRange(20, 8)];
[labelCF setFont:[UIFont fontWithName:@"Courier-Bold" size:14] range:NSMakeRange(20, 8)];
[labelCF setTextColor:[UIColor cyanColor] range:NSMakeRange(33, 11)];
[labelCF setFont:[UIFont fontWithName:@"Courier-Oblique" size:14] range:NSMakeRange(33, 11)];
[self.view addSubview:labelCF]; }

Demo:https://files.cnblogs.com/files/sixindev/UILabelFormattedText.zip

附录:

//常见的属性及说明
NSFontAttributeName 设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
NSForegroundColorAttributeNam 设置字体颜色,取值为 UIColor对象,默认值为黑色
NSBackgroundColorAttributeName 设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
NSLigatureAttributeName 设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
NSKernAttributeName 设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
NSStrikethroughStyleAttributeName 设置删除线,取值为 NSNumber 对象(整数)
NSStrikethroughColorAttributeName 设置删除线颜色,取值为 UIColor 对象,默认值为黑色
NSUnderlineStyleAttributeName 设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
NSUnderlineColorAttributeName 设置下划线颜色,取值为 UIColor 对象,默认值为黑色
NSStrokeWidthAttributeName 设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
NSStrokeColorAttributeName 填充部分颜色,不是字体颜色,取值为 UIColor 对象
NSShadowAttributeName 设置阴影属性,取值为 NSShadow 对象
NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
NSBaselineOffsetAttributeName 设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
NSObliquenessAttributeName 设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
NSExpansionAttributeName 设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
NSWritingDirectionAttributeName 设置文字书写方向,从左向右书写或者从右向左书写
NSVerticalGlyphFormAttributeName 设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址
NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
NSParagraphStyleAttributeName 设置文本段落排版格式,取值为 NSParagraphStyle 对象

  

扩展阅读

ios知识点总结——富文本实现图文混排

FastTextView--iOS上最好的开源富文本编辑器。

TYAttributedLabel

图文混排NSTextAttachment

http://itfish.net/article/37083.html

http://joeytat.com/post/ios/7.-shi-yong-%60nsattributedstring%60-shi-xian-tu-wen-hun-pai

AttributeText创建多彩文字Label --- hl的更多相关文章

  1. 点击文字label同时选中checkbox radio

    在做网页的时候一般会有一个需求:点击一段文字信息的同时选中某个checkbox 旧处理方式是在这段文字上加上点击事件触发checkbox的选中事件 //jq中://选中 $("#ID&quo ...

  2. Android TextView设置多彩文字

    在应用开发中时常会遇到需要在一段文字中插入几个不一样颜色文字的需求; 以前本人都是用多个TextView拼起来,不仅感觉很蠢,操作起来也蛮恶心; 直到接触到了SpannableStringBuilde ...

  3. google地图marker文字label添加js lib

    google的地图marker需要使用js开发库,文件并允许使用js库 在JSP页面中需要添加地图引用如: <script src="http://maps.googleapis.co ...

  4. Cocos2D创建多彩文本显示标签

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Cocos2D中默认的CCLableTTF类从源代码里看是支持 ...

  5. ObjectARX创建带文字的线型实例代码

    AcDbLinetypeTable* pLinetypeTable=NULL; Acad::ErrorStatus es = acdbHostApplicationServices()->wor ...

  6. NSAttributeString创建各种文字效果

    NSDictionary *attributes =@{ NSForegroundColorAttributeName: [UIColorredColor], NSFontAttributeName: ...

  7. 【Cocos2d-x for WP8 学习整理】(5)文字显示全整理

    学习 Cocos2d-x 有一阵了,现在要做个小东西,第一步就遇到了文字展示的问题,于是想把可能遇到的问题统统整理一下.这一部分也不局限于wp8,全平台应该都是一个解决方案. 先在脑袋里大致想了一下, ...

  8. iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片

    Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...

  9. iOS开发——项目篇—高仿百思不得姐 05——发布界面、发表文字界面、重识 bounds、frame、scrollView

    加号界面(发布模块) 一.点击加号modal出发布模块,创建控件,布局控件1)使用xib加载view,如果在viewDidLoad创建控件并设置frame 那么self.view 的宽高 拿到的是xi ...

随机推荐

  1. Java程序设计基础笔记 • 【第9章 方法与参数类型】

    全部章节   >>>> 本章目录 9.1 有参数有返回值的方法 9.1.1 有参数有返回值的方法的定义和调用 9.1.2 返回值的传递过程 9.1.3 方法返回值的注意事项 9 ...

  2. ActiveMQ基础教程(二):安装与配置(单机与集群)

    因为本文会用到集群介绍,因此准备了三台虚拟机(当然读者也可以使用一个虚拟机,然后使用不同的端口来模拟实现伪集群): 192.168.209.133 test1 192.168.209.134 test ...

  3. Star Way To Heaven

    题目描述 小 x伤心的走上了 Star way to heaven. 到天堂的道路是一个笛卡尔坐标系上一个 n*m的长方形通道 顶点在0,0 和 . 小 n,m 从最左边任意一点进入,从右边任意一点走 ...

  4. APP自动化,怎样让应用不重置?

    noReset =True产生的背景: 在编写APP自动化代码时,除了登录用例需要填写账号和密码外,其余很多用例都是需要先登录再操作的,如果每一个用例都从头开始到具体的操作,这样将会耗费很多时间,此时 ...

  5. spring boot 单元测试 --- 在测试类使用 javabean注解操作接口

    1.依赖包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...

  6. Spark案例练习-打包提交

    关注公众号:分享电脑学习回复"百度云盘" 可以免费获取所有学习文档的代码(不定期更新)云盘目录说明:tools目录是安装包res 目录是每一个课件对应的代码和资源等doc 目录是一 ...

  7. Linux防火墙--IPtables企业级配置策略思路

    一.防火墙简介 防火墙定义:是通过有机结合各类用于安全管理与筛选的软件和硬件设备,帮助计算机网络于其内.外网之间构建一道相对隔绝的保护屏障,以保护用户资料与信息安全性的一种技术. 防火墙发展应用:最早 ...

  8. 【Java】==与equals

    ==与equals 一. == 可以使用在基本数据类型变量和引用数据类型变量中 如果比较的是基本数据类型变量:比较两个变量保存的数据是否相等.(不一定类型要相同) 如果比较的是引用数据类型变量:比较两 ...

  9. 《剑指offer》面试题63. 股票的最大利润

    问题描述 假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖该股票一次可能获得的最大利润是多少? 示例 1: 输入: [7,1,5,3,6,4] 输出: 5 解释: 在第 2 天(股票价格 = ...

  10. LaTex 中圆圈序号及一些特殊字符的输入

    众所周知,LATEX 提供了 \textcircled 命令用以给字符加圈,但效果却不怎么好: 实际上,加圈并不是一个平凡的变换,它会涉及到圈内字符形状的微调,而这是几乎无法在 TEX 宏层面解决的. ...