#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(富文本)的简单使用的更多相关文章

  1. UEditor富文本编辑器简单使用

    UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...

  2. iOS - NSMutableAttributedString富文本的实现

    NSMutableAttributedString继承于NSAttributedString(带属性的字符串)能够简单快速实现富文本的效果;不多说直接上效果图和代码,通俗易懂: (一)效果图: (二) ...

  3. UILabel添加图片之富文本的简单应用

    若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...

  4. NSMutableAttributedString 富文本的使用

    //富文本的使用 UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; testLabel.backgroun ...

  5. ios富文本的简单使用 AttributedString

    富文本,顾名思义就是丰富的文本格式,本文demo使用NSMutableAttributedString //获取富文本 NSMutableAttributedString*attributeStrin ...

  6. (转)解决NSMutableAttributedString富文本,不同文字大小水平轴对齐问题(默认底部对齐)

    默认是底部对齐,其实对的也不齐, 目标效果:  代码: NSBaselineOffsetAttributeName 基线偏移量: 调整: NSBaselineOffsetAttributeName的值 ...

  7. iOS - UILabel添加图片之富文本的简单应用

    //创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...

  8. Vue系列:wangEditor富文本编辑器简单例子

    考虑到该富文本编辑器可能会在后续项目中继续使用,因此单独将其做成一个组件,把wangeditor作为组件的形式使用. 以下是参考代码 子组件部分: 父组件引用子组件: 以上就是 wangEditor ...

  9. NSMutableAttributedString 富文本删除线的用法

    #import <UIKit/UIKit.h> //价格 NSString *priceStr = @"99元 剁手价66元"; NSMutableAttributed ...

随机推荐

  1. iOS 详解NSXMLParser方法解析XML数据方法

    前一篇文章已经介绍了如何通过URL从网络上获取xml数据.下面介绍如何将获取到的数据进行解析. 下面先看看xml的数据格式吧! <?xml version="1.0" enc ...

  2. 背景透明文字不透明的最佳方法兼容IE(以背景黑色透明度0.5为例)

    以背景黑色,透明度0.5举例为大家详细介绍下关于背景透明,文字不透明的最佳方法同时兼容IE,具体实现如下,感兴趣的朋友可以参考下哈希望对大家有所帮助 以背景黑色,透明度0.5举例 非IE:backgr ...

  3. VB操作CAD

    Dim xlapp As Excel.Application            Dim xlbook As Excel.Workbook            Dim sheet As Excel ...

  4. iOS7 人机界面设计指南

    iOS7 人机界面设计指南     苹果在WWDC 2013大会上发布了iOS 7,新系统一改5年来的拟物路线,在乔纳森•艾维的主导下,加入了更多的“扁平化”和“极简”现代设计元素. iOS7系统界面 ...

  5. Android设计模式系列-单例模式

    单例模式,可以说是GOF的23种设计模式中最简单的一个. 这个模式相对于其他几个模式比较独立,它只负责控制自己的实例化数量单一(而不是考虑为用户产生什么样的实例),很有意思,是一个感觉上很干净的模式, ...

  6. cocos2d 场景转换的方法执行顺序

    转自:http://shanbei.info/the-cocos2d-scene-conversion-method-execution-order.html 如果你希望在场景转换的过程中使用过渡效果 ...

  7. ThinkPHP Volist标签

    Volist标签主要用于在模板中循环输出数据集或者多维数组. volist标签(循环输出数据) 闭合 非闭合标签 属性 name(必须):要输出的数据模板变量 id(必须):循环变量 offset(可 ...

  8. 学习笔记之Lucene

    http://baike.baidu.com/view/371811.htm?fr=aladdin Apache Lucene(http://lucene.apache.org/) Java 全文搜索 ...

  9. Ubuntu10.04下载并编译Android4.3源代码

    注:转载或引用请标明出处    http://blog.csdn.net/luzhenrong45/article/details/9719433 去年用Ubuntu10.10成功下载并编译Andro ...

  10. 在aws ec2上使用root用户登录

    aws ec2默认是使用ec2-user账号登陆的,对很多文件夹是没有权限的.如何使用root账号执行命令就是一个问题了.解决办法如下: 1.根据官网提供的方法登录连接到EC2服务器(官网推荐wind ...