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 ...
随机推荐
- Tomcat服务器常用配置和HTTP简介
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- 使用百度地图结合GPS进行定位
本文在上文基础上加入GPS定位功能,实现实时定位,代码如下: Activity: package com.home; import android.app.Activity; import andro ...
- Object.defineProperty vs __defineGetter__ vs normal
Testing in Chrome 31.0.1650.63 32-bit on Windows Server 2008 R2 / 7 64-bit Test Ops/sec Object.defin ...
- 在CentOS上编译安装PostgreSQL
http://my.oschina.net/tashi/blog 第一步:准备阶段 获取必需软件包: CentOS中查看是否安装了某个软件的命令:rpm -qa | grep 软件名.which命令可 ...
- cocos2d 小游戏
今天写了一个小游戏,发现看过的代码自己来写还是会经常出错,还是要多自己动手写写哈. 先上几张游戏界面图 void HelloWorld::addTarget() { //首先初始化精灵 CCSprit ...
- unix 网路编程(卷一)第一个程序编译过程
unix卷一去年暑假买的到现在才开始看无比惭愧,而且惭愧第一个程序就断断续续弄了几天,要好好写程序了,马上要找工作了,下面介绍下把本书第一个程序跑起来的过程: 搜各种博客 我用系统的是ubuntu 1 ...
- USB HID usage table
This usage table lets usbhidctl decode the HID data correctly for the APC RS/XS1000's. This work was ...
- linux 学习笔记 GNU工具链简介
我们通常无法直接通过Linux内核,而需要借助Linux内核之上的GUN工具链来进行 文件处理 文本操作 进程管理 等操作. GNU/Linux shell为用户提供了 启动程序 管理文件系统上的文件 ...
- linux 命令学习日记 20160621
1.cat /proc/meminfo 观察Linux系统上虚拟内存的当前状态 2.ipcs -m ipcs命令专门用来查看系统上的当前共享内存页面 3.ls /dev -al sda* ttyS* ...
- MVC中使用QrCodeNet 生成二维码
QrCodeNet下载地址:http://qrcodenet.codeplex.com/ using System.Drawing; using System.Drawing.Imaging; usi ...