简易使用UILabel的富文本
简易使用UILabel的富文本

使用效果:

源码:
NSString+YX.h NSString+YX.m
//
// NSString+YX.h
// YXKit
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <Foundation/Foundation.h>
#import "ConfigAttributedString.h" @interface NSString (YX) // 创建富文本并配置富文本(NSArray中的数据必须是ConfigAttributedString对象合集)
- (NSMutableAttributedString *)createAttributedStringAndConfig:(NSArray *)configs; // 用于搜寻一段字符串在另外一段字符串中的NSRange值
- (NSRange)rangeFrom:(NSString *)string; // 本字符串的range
- (NSRange)range; @end
//
// NSString+YX.m
// YXKit
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "NSString+YX.h" @implementation NSString (YX) - (NSMutableAttributedString *)createAttributedStringAndConfig:(NSArray *)configs
{
NSMutableAttributedString *attributedString = \
[[NSMutableAttributedString alloc] initWithString:self]; [configs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
ConfigAttributedString *oneConfig = obj;
[attributedString addAttribute:oneConfig.attribute
value:oneConfig.value
range:oneConfig.range];
}]; return attributedString;
} - (NSRange)rangeFrom:(NSString *)string
{
return [string rangeOfString:self];
} - (NSRange)range
{
return NSMakeRange(, self.length);
} @end
ConfigAttributedString.h ConfigAttributedString.m
//
// ConfigAttributedString.h
// NSMutableAttributedString
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <Foundation/Foundation.h> @interface ConfigAttributedString : NSObject @property (nonatomic, strong, readonly) NSString *attribute; // 富文本属性
@property (nonatomic, strong, readonly) id value; // 富文本值
@property (nonatomic, assign, readonly) NSRange range; // 富文本范围值 // 通用型配置
+ (instancetype)attribute:(NSString *)attribute
value:(id)value
range:(NSRange)range; // 配置字体
+ (instancetype)font:(UIFont *)font
range:(NSRange)range; // 配置字体颜色
+ (instancetype)foregroundColor:(UIColor *)color
range:(NSRange)range; // 配置字体背景颜色
+ (instancetype)backgroundColor:(UIColor *)color
range:(NSRange)range; // 字体描边颜色以及描边宽度以及阴影(以下两个方法可以一起使用)
+ (instancetype)strokeColor:(UIColor *)color
range:(NSRange)range;
+ (instancetype)strokeWidth:(float)number
range:(NSRange)range;
+ (instancetype)shadow:(NSShadow *)shadow
range:(NSRange)range; // 配置文字的中划线
+ (instancetype)strikethroughStyle:(NSInteger)number
range:(NSRange)range; // 配置文字的下划线
+ (instancetype)underlineStyle:(NSInteger)number
range:(NSRange)range; // 字间距
+ (instancetype)kern:(float)number
range:(NSRange)range; // 段落样式(需要将UILabel中的numberOfLines设置成0才有用)
+ (instancetype)paragraphStyle:(NSMutableParagraphStyle *)style
range:(NSRange)range; @end
//
// ConfigAttributedString.m
// NSMutableAttributedString
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "ConfigAttributedString.h" @interface ConfigAttributedString () @property (nonatomic, strong) NSString *attribute;
@property (nonatomic, strong) id value;
@property (nonatomic, assign) NSRange range; @end @implementation ConfigAttributedString + (instancetype)attribute:(NSString *)attribute value:(id)value range:(NSRange)range
{
ConfigAttributedString *config = [self new]; config.attribute = attribute;
config.value = value;
config.range = range; return config;
} + (instancetype)font:(UIFont *)font range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSFontAttributeName;
config.value = font;
config.range = range; return config;
} + (instancetype)foregroundColor:(UIColor *)color range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSForegroundColorAttributeName;
config.value = color;
config.range = range; return config;
} + (instancetype)backgroundColor:(UIColor *)color range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSBackgroundColorAttributeName;
config.value = color;
config.range = range; return config;
} + (instancetype)strikethroughStyle:(NSInteger)number range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSStrikethroughStyleAttributeName;
config.value = [NSNumber numberWithInteger:number];
config.range = range; return config;
} + (instancetype)paragraphStyle:(NSMutableParagraphStyle *)style range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSParagraphStyleAttributeName;
config.value = style;
config.range = range; return config;
} + (instancetype)kern:(float)number range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSKernAttributeName;
config.value = [NSNumber numberWithFloat:number];
config.range = range; return config;
} + (instancetype)underlineStyle:(NSInteger)number range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSUnderlineStyleAttributeName;
config.value = [NSNumber numberWithInteger:number];
config.range = range; return config;
} + (instancetype)strokeColor:(UIColor *)color range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSStrokeColorAttributeName;
config.value = color;
config.range = range; return config;
} + (instancetype)strokeWidth:(float)number range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSStrokeWidthAttributeName;
config.value = [NSNumber numberWithFloat:number];
config.range = range; return config;
} + (instancetype)shadow:(NSShadow *)shadow range:(NSRange)range
{
ConfigAttributedString *config = [self new];
config.attribute = NSShadowAttributeName;
config.value = shadow;
config.range = range; return config;
} @end
控制器源码:
//
// RootViewController.m
// RichText
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "NSString+YX.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 字符串
NSString *str = @"未选择的路-弗罗斯特\n黄色的树林里分出两条路,\n可惜我不能同时去涉足,\n我在那路口久久伫立,\n我向着一条路极目望去,\n直到它消失在丛林深处。\n但我却选了另外一条路,\n它荒草萋萋,十分幽寂,\n显得更诱人、更美丽,\n虽然在这两条小路上,\n都很少留下旅人的足迹,\n虽然那天清晨落叶满地,\n两条路都未经脚印污染。\n啊,留下一条路等改日再见!\n但我知道路径延绵无尽头,\n恐怕我难以再回返。\n也许多少年后在某个地方,\n我将轻声叹息把往事回顾,\n一片树林里分出两条路,\n而我选了人迹更少的一条,\n从此决定了我一生的道路。"; // 设置组
NSArray *array = \
@[// 全局设置
[ConfigAttributedString font:[UIFont systemFontOfSize:.f] range:[str range]],
[ConfigAttributedString paragraphStyle:[self style] range:[str range]], // 局部设置
[ConfigAttributedString foregroundColor:[UIColor redColor]
range:[@"未选择的路" rangeFrom:str]],
[ConfigAttributedString font:[UIFont systemFontOfSize:.f]
range:[@"未选择的路" rangeFrom:str]]]; // 初始化富文本
UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];
label.numberOfLines = ;
label.attributedText = [str createAttributedStringAndConfig:array];
[self.view addSubview:label];
} // 段落样式
- (NSMutableParagraphStyle *)style
{
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
style.lineSpacing = .f;
style.firstLineHeadIndent = .f; return style;
} @end
设计原理:
我把配置抽象成了一个对象,一个对象可能对应着不同的配置,这样写起来就不会乱

使用的时候直接就在数组中配置

通过NSString的Category直接生成富文本赋值给attributedText即完成了富文本的操作

这才叫面向对象编程嘛:)
简易使用UILabel的富文本的更多相关文章
- UILabel的富文本显示选项
UILabel的富文本格式设置 1.实例化方法和使用方法 实例化方法: 使用字符串初始化 - (id)initWithString:(NSString *)str; 例: NSMutableAttri ...
- UILabel设置富文本后不显示省略号
先描述一下问题,项目中用到了UILabel去显示一段富文本文字,超过label显示区域部分,省略号处理. 但是当设置好 attributedText 给label之后,显示出的效果是文字被切割了,并没 ...
- UILabel设置富文本格式显示
实例化方法和使用方法 实例化方法: 使用字符串初始化 - (id)initWithString:(NSString *)str; 例: NSMutableAttributedString *Attri ...
- UILabel富文本 段落格式以及UILabel添加图片
之前文本整理有一点乱,这边重新整理一下,下面是效果图,一共两个UILabel, 富文本整理: /*NSForegroundColorAttributeName设置字体颜色,对象UIColor; NSP ...
- UILabel添加图片之富文本的简单应用
若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...
- UIlabel - 富文本属性
1.NSKernAttributeName: @10 调整字句 kerning 字句调整 2.NSFontAttributeName : [UIFont systemFontOfSize:_fontS ...
- 简易富文本编辑器bootstrap-wysiwyg源码注释
好久没写随笔了,因为最近比较忙,小公司基本都是一个前端干所有属于和部分不属于前端的事情,所以就没空弄了,即使想分享,也因为没有时间和精力就搁置了. 这周周六日休息,正好时间比较充裕(ps:目前处在单休 ...
- UILabel(富文本)
本文转载至 http://www.jianshu.com/p/5d24d22f99c3 富文本 NSString *str = @"人生若只如初见,何事秋风悲画扇.\n等闲变却故人心,却道故 ...
- iOS UIlabel怎么加载html字符串 富文本的用法
要加载html字符串,用人说,直接用webView啊!但是,有时候我们只需要显示2行文字,如此少的内容却要在复杂的UI排版中加入一个占用资源较多的webview,得不偿失.这里要说的是,我们其实可以用 ...
随机推荐
- Word2vec 理解
1.有DNN做的word2vec,取隐藏层到softmax层的权重为词向量,softmax层的叶子节点数为词汇表大小 2-3的最开始的词向量是随机初始化的 2.哈夫曼树:左边走 sigmoid(当前节 ...
- Linux-(diff)
diff 命令 1.命令格式: diff [参数] [文件1或目录1] [文件2或目录2] 2.命令功能: diff命令能比较单个文件或者目录内容.如果指定比较的是文件,则只有当输入为文本文件时 ...
- ibatis中的cdata和xml中cdata的含义
ibatis的cdata用于sqlmap文件中,二sqlmap本身就是xml文件,即解析cdata的方法与xml文件的cdata相同. 简单来说:cdata就是用来表明纯文本的,如果没有这个的话 &l ...
- 学习ThinkPHP笔记
学习ThinkPHP笔记 TP的模块化设计 名称 描述 应用 基于同一个入口文件访问的项目我们称之为一个应用. 模块 一个应用下面可以包含多个模块,每个模块在应用目录下面都是一个独立的子目录. 控制器 ...
- 【转】CSS浮动(float,clear)通俗讲解
作者:杨元 本文链接:http://www.cnblogs.com/iyangyuan/archive/2013/03/27/2983813.html 很早以前就接触过CSS,但对于浮动始终非常迷惑, ...
- django2.1---上下文处理器
上下文处理器 上下文处理器是可以返回一些数据,在全局模板中都可以使用.比如登录后的用户信息,在很多页面中都需要使用,那么我们可以放在上下文处理器中,就没有必要在每个视图函数中都返回这个对象. 在set ...
- mysql 用存储过程和函数分别模拟序列
在其他大部分DBMS里都有序列的概念,即Sequence或Generator. 而mysql里没有,但有时真的很有用.下面分别用存储过程和函数来模拟序列,并用程序模拟并发场景来测试原子性和完整性,是否 ...
- 傻瓜式解读koa中间件处理模块koa-compose
最近需要单独使用到koa-compose这个模块,虽然使用koa的时候大致知道中间件的执行流程,但是没仔细研究过源码用起来还是不放心(主要是这个模块代码少,多的话也没兴趣去研究了). koa-comp ...
- Hibernate的一对多实例
一对多在现实生活中很常见,今天做了个Hibernate的一对多的实例,也是个入门过程,写下来跟大家分享. 最重要的是xml配置文件,之前因为把英文"(引号)错误的复制成中文的“”(引号),导 ...
- MySQL 报错
SELECT COUNT(1) FROM (select tid from teacher where tname like '李%') 1248 - Every derived table must ...