NSMutableAttributedString(富文本)的简单使用
#import "ViewController.h"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width-, self.view.frame.size.height--)];
label.text = @"春种一粒粟,秋成万颗子。\n四海无闲田,农夫犹饿死。\n锄禾日当午,汗滴禾下土。\n谁知盘中餐,粒粒皆辛苦。";
[self.view addSubview:label];
NSRange rangeOne = [label.text rangeOfString:@"春种一粒粟"];
NSRange rangeTwo = [label.text rangeOfString:@"秋成万颗子"];
NSRange rangeThree = [label.text rangeOfString:@"四海无闲田"];
NSRange rangeFour = [label.text rangeOfString:@"农夫犹饿死"];
NSRange rangeFive = [label.text rangeOfString:@"锄禾日当午"];
NSRange rangeSix = [label.text rangeOfString:@"汗滴禾下土"];
NSRange rangeSeven = [label.text rangeOfString:@"谁知盘中餐"];
NSRange rangeEight = [label.text rangeOfString:@"粒粒皆辛苦"];
NSMutableAttributedString *attributedLabel = [[NSMutableAttributedString alloc] initWithString:label.text];
//字体
[attributedLabel addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:] range:NSMakeRange(, label.text.length)];
//段落
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = ; //行间距
paragraphStyle.alignment = NSTextAlignmentCenter;
[attributedLabel addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(, label.text.length)];
//字体颜色
[attributedLabel addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeOne.location, rangeOne.length)];
//字体底色
[attributedLabel addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeTwo.location, rangeTwo.length)];
//删除线
[attributedLabel addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:] range:NSMakeRange(rangeThree.location, rangeThree.length)];
[attributedLabel addAttribute:NSStrikethroughColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeThree.location, rangeThree.length)];
//下划线
[attributedLabel addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:] range:NSMakeRange(rangeFour.location, rangeFour.length)];
[attributedLabel addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeFour.location, rangeFour.length)];
//文字间距
[attributedLabel addAttribute:NSKernAttributeName value:[NSNumber numberWithInt:] range:NSMakeRange(rangeFive.location, rangeFive.length)];
//字体倾斜(正值右倾,负值左倾)
[attributedLabel addAttribute:NSObliquenessAttributeName value:[NSNumber numberWithFloat:0.5] range:NSMakeRange(rangeSix.location, rangeSix.length)];
//笔画宽度(正值中空,负值填充)
[attributedLabel addAttribute:NSStrokeWidthAttributeName value:[NSNumber numberWithFloat:] range:NSMakeRange(rangeSeven.location, rangeSeven.length)];
//填充颜色
[attributedLabel addAttribute:NSStrokeColorAttributeName value:[UIColor redColor] range:NSMakeRange(rangeSeven.location, rangeSeven.length)];
//阴影效果
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor redColor];
shadow.shadowOffset = CGSizeMake(, );
shadow.shadowBlurRadius = ;
[attributedLabel addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(rangeEight.location, rangeEight.length)];
//自适应高
label.numberOfLines = ;
CGRect labelStringRect = [attributedLabel boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*, ) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.height = labelStringRect.size.height;
label.frame = labelRect; label.attributedText = attributedLabel;
}
效果图如下:

NSMutableAttributedString(富文本)的简单使用的更多相关文章
- UEditor富文本编辑器简单使用
UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...
- iOS - NSMutableAttributedString富文本的实现
NSMutableAttributedString继承于NSAttributedString(带属性的字符串)能够简单快速实现富文本的效果;不多说直接上效果图和代码,通俗易懂: (一)效果图: (二) ...
- UILabel添加图片之富文本的简单应用
若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...
- NSMutableAttributedString 富文本的使用
//富文本的使用 UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; testLabel.backgroun ...
- ios富文本的简单使用 AttributedString
富文本,顾名思义就是丰富的文本格式,本文demo使用NSMutableAttributedString //获取富文本 NSMutableAttributedString*attributeStrin ...
- (转)解决NSMutableAttributedString富文本,不同文字大小水平轴对齐问题(默认底部对齐)
默认是底部对齐,其实对的也不齐, 目标效果: 代码: NSBaselineOffsetAttributeName 基线偏移量: 调整: NSBaselineOffsetAttributeName的值 ...
- iOS - UILabel添加图片之富文本的简单应用
//创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...
- Vue系列:wangEditor富文本编辑器简单例子
考虑到该富文本编辑器可能会在后续项目中继续使用,因此单独将其做成一个组件,把wangeditor作为组件的形式使用. 以下是参考代码 子组件部分: 父组件引用子组件: 以上就是 wangEditor ...
- NSMutableAttributedString 富文本删除线的用法
#import <UIKit/UIKit.h> //价格 NSString *priceStr = @"99元 剁手价66元"; NSMutableAttributed ...
随机推荐
- RS485连接CAN——应急用法【worldsing笔记】【待完善】
阅读前提:假设读者对CAN总线和485总线有一定了解. RX485连接CAN用法提出背景: 在一般情况下只能是CAN对CAN 485对485, 但是在调试过程中难免对出现设备没有CAN接口,或是没有4 ...
- SQL 索引
1.http://www.cnblogs.com/AK2012/archive/2013/01/04/2844283.html 2 .聚簇索引和非聚簇索引的区别 3.聚集索引:只能有一个 (相当于字 ...
- Unity3D之AssetBundle学习:Android上运行笔记
路径统一 在Android上加载StreamingAssets文件夹下的AssetBundle文件,首先需要对加载地址进行处理,注意PC.Android和IOS的地址不一致需要针对不同的平台不同的处理 ...
- 导出cluster log
将所有群集节点的日志导出到 clog 目录下: get-clusterlog -destination c:\clog 只导出前10分钟的群集日志: get-cluster -destination ...
- 【转】使用junit进行单元测试(初级篇)
转自:http://blog.csdn.net/andycpp/article/details/1327147 我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能很强大,但我们在 ...
- 从Eclipse到Android Studio经历
现在不得不要和相处近两年的Eclipse分手了,很舍不得,谢谢你这些日子有你的陪伴,每天都会有些的期待和挑战.两年来,我们建立了很深厚的情感.曾经以为我的世界只能有你,而现在我的心里可能有了别人.起初 ...
- iOS开发——面试笔试精华(二)
面试笔试精华(二) 警告:一定要把英文题目过一遍,有些公司的题目故意弄成英文的!!! 1. Difference between shallow copy and deep copy? ...
- IPVS
http://kb.linuxvirtualserver.org/wiki/IPVS_FULLNAT_and_SYNPROXY
- qt 总结
Qt中的每个类,都有一个对应的同名头文件,其中包含其类定义.例如要使用QApplication类,则需要在程序中添加" #include <QApplication>" ...
- python列表删除重复元素的三种方法
给定一个列表,要求删除列表中重复元素. listA = ['python','语','言','是','一','门','动','态','语','言'] 方法1,对列表调用排序,从末尾依次比较相邻两个元素 ...