GONMarkupParser的使用

说明

这是一个写得非常好的富文本工具类,便于你进行简易的封装。本人抛砖引玉,只进行了少量的简化使用封装。

效果

源码

https://github.com/nicolasgoutaland/GONMarkupParser

//
// NSString+MarkupParserString.h
// RichText
//
// Created by YouXianMing on 15/5/14.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> @interface NSString (MarkupParserString) /**
* 添加颜色标识
*
* @param mark 颜色标识
*
* @return 添加好的文本
*/
- (NSString *)addColorMark:(NSString *)mark; /**
* 添加字体标识
*
* @param name 字体名字(如果为空则为系统字体)
* @param size 字体大小
*
* @return 添加好的文本
*/
- (NSString *)addFontMarkWithFontName:(NSString *)name
size:(CGFloat)size; /**
* 添加链接标识
*
* @param mark 链接标识
*
* @return 添加好的文本
*/
- (NSString *)addLinkMark:(NSString *)mark; /**
* 创建富文本
*
* @return 创建好的富文本
*/
- (NSAttributedString *)createAttributedString; @end
//
// NSString+MarkupParserString.m
// RichText
//
// Created by YouXianMing on 15/5/14.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "NSString+MarkupParserString.h"
#import "GONMarkupParser_All.h" @implementation NSString (MarkupParserString) - (NSString *)addColorMark:(NSString *)mark { // <color value="red">text</>
// <color value="#FFEEAA">text</>
// <color value="myCustomRegisteredColor">text</> return [NSString stringWithFormat:@"<color value=\"%@\">%@</>", mark, self];
} - (NSString *)addFontMarkWithFontName:(NSString *)name
size:(CGFloat)size { // Define a generic markup to configure font
// "size" is used to define font size
// If missing, current font size will be used. If not found is set, default system font size will be used
// "name" define a registered font name or full font name
// - Markup will first try to find a registeredFont with given name.
// - If no font found, markup will try to load a font using given name
// If "name" isn't set, current defined font will be used with new defined size. If no font is currently used, default system one will be used
//
// If no attribute is set, current defined font will be removed (NSFontAttributeName), and default system one will be used instead
//
// Examples
//
// <font size="18">This text will use current font, set to 18</>
// <font name="Helvetica">This text will use Helvetica as font, using current font size</>
// <font name="Helvetica" size="18">This text will use Helvetica, set to 18</> if (name == nil) {
return [NSString stringWithFormat:@"<font size=\"%f\">%@</>", size, self];
} else {
return [NSString stringWithFormat:@"<font name=\"%@\" size=\"%f\">%@</>", name, size, self];
}
} - (NSString *)addLinkMark:(NSString *)mark { // To detect user touch on link :
// - display attributed string in a UITextView
// - configure UITextView selectable property to YES and isEditable to NO
// - set delegate
// - implement "textView:shouldInteractWithURL:inRange:" method
//
// Examples
//
// <a href="#1">Link 1</>
// <a href="http://www.apple.com">Link to apple.com</>
// <a href="myscheme://myapp">Custom link</> return [NSString stringWithFormat:@"<a href=\"%@\">%@</>", mark, self];
} - (NSAttributedString *)createAttributedString {
return [[GONMarkupParserManager sharedParser] attributedStringFromString:self
error:nil];
} @end
//
// ViewController.m
// RichText
//
// Created by YouXianMing on 15/5/14.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "NSString+MarkupParserString.h" @interface ViewController ()
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSTimer *timer;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 创建label
self.label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
self.label.center = self.view.center;
[self.view addSubview:self.label]; // 定时器
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1f
target:self
selector:@selector(timerEvent)
userInfo:nil
repeats:YES];
} - (void)timerEvent {
NSString *num = [NSString stringWithFormat:@"%d", (arc4random() % )];
NSString *name = [[@"YouXianMing" addFontMarkWithFontName:@"Avenir-MediumOblique" size:] addColorMark:@"red"];
NSString *age = [[num addFontMarkWithFontName:@"Avenir-MediumOblique" size:.f] addColorMark:@"gray"]; // 加载富文本
self.label.attributedText = [[NSString stringWithFormat:@" %@ %@", name, age] createAttributedString];
} @end

细节

GONMarkupParser的使用的更多相关文章

  1. [翻译] GONMarkupParser

    GONMarkupParser https://github.com/nicolasgoutaland/GONMarkupParser NSString *inputText = @"Sim ...

  2. iOS之UI--富文本总结

    文章内容大纲 1.NSMutableAttributedString的基本使用 2.NSMutableAttributedString的简易封装 3.使用开源代码GOBMarkupPaser处理富文本 ...

  3. iOS富文本-NSAttributedString简单封装

    直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 ) {         ; i < styles.count; i ++) {      ...

随机推荐

  1. Linux命令-用户、用户组、权限

    参考资料: http://www.linuxidc.com/Linux/2014-07/104445.htm    Linux入门教程:如何手动创建一个Linux用户 http://www.linux ...

  2. 理解Linux内核之中断控制

    乍一看下边的Linux内核代码,貌似L3389有bug,于是我就绕有兴趣地阅读了一下local_irq_save/local_irq_restore的源代码. /* linux-4.14.12/mm/ ...

  3. Python远程连接主机之paramiko模块

    Python的paramiko模块能够连接远程主机,并在该主机上执行命令,和该主机之间进行文件传输.paramiko支持用明文密码登录远程主机和秘钥登录.使用之前要安装一下这个模块哈,pip inst ...

  4. 这个PHP无解深坑,你能解出来吗?(听说能解出来的都很秀)

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由horstxu发表于云+社区专栏 1. 问题背景 PHP Laravel框架中的db migration是比较常用的一个功能了.在每个 ...

  5. Golang 并发Groutine实例解读(二)

    go提供了sync包和channel机制来解决协程间的同步与通信. 一.sync.WaitGroup sync包中的WaitGroup实现了一个类似任务队列的结构,你可以向队列中加入任务,任务完成后就 ...

  6. C#中的序列化和反序列化是什么、有什么作用、使用方法详解

    什么是序列化与反序列化??? 序列化和反序列化,我们可能经常会听到,其实通俗一点的解释,序列化就是把一个对象保存到一个文件或数据库字段中去,反序列化就是在适当的时候把这个文件再转化成原来的对象使用. ...

  7. Firebird 备份与恢复

    备份test gbak -b test d:\test.bak -user sysdba 恢复为test1 gbak -c d:\test.bak test1

  8. sqlserver中调用服务器中的webservice接口

    declare @ServiceUrl as varchar(1000) declare @UrlAddress varchar(500)--WebService地址:以http开头,结尾带斜杠,例如 ...

  9. Python__random库基本介绍

    random库是使用随机数的Python标准库 从概率论角度来说,随机数是随机产生的数据(比如抛硬币),但时计算机是不可能产生随机值,真正的随机数也是在特定条件下产生的确定值,只不过这些条件我们没有理 ...

  10. 哪个类可用于处理 Unicode?

    A. InputStreanReader的构造函数: InputStreamReader(InputStream in)          创建一个使用默认字符集的 InputStreamReader ...