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. C++字符串string类常用操作详解(一)【初始化、遍历、连接】

    代码示例: #include <iostream> #include "string" using namespace std; //字符串初始化 void strIn ...

  2. windows server服务器上部署java+tomcat网站域名配置

    如果只是部署java项目的话,可以把IIS删除,然后在服务器上安装jdk tomcat 配置好环境变量,就和你在自己计算机上开发一样,把你的项目war包拷到tomcat下的webapps里(任意目录都 ...

  3. 回溯法求解n皇后和迷宫问题

    回溯法是一种搜索算法,从某一起点出发按一定规则探索,当试探不符合条件时则返回上一步重新探索,直到搜索出所求的路径. 回溯法所求的解可以看做解向量(n皇后坐标组成的向量,迷宫路径点组成的向量等),所有解 ...

  4. IOS开发常见第三方总结

    链接](https://github.com/languages​​/Objective-C/most_watched) * [three20](https://github.com/facebook ...

  5. Linux系统资源管理 之 硬件信息

    1. CPU lscpu : 一般不加参数,直接使用该命令. cat /proc/cpuinfo: 该文件中列出了CPU的详细信息,类似于'lscpu'命令 lscpu [niesh @niesh D ...

  6. java开学考试感想及代码

    上周四我们的第一节java课,王老师给我们进行啦开学考试,这也是上学期放假之前给我们约定好的 但是情况显然没有我想的那么好,其实我觉得这个atm系统比上届学生信息管理系统难的多,上届的话毕竟有小学期的 ...

  7. [转] Lodop、C-Lodop使用说明及样例

    本文转自:http://www.lodop.net/LodopDemo.html Lodop(标音:劳道谱,俗称:露肚皮)是专业WEB控件,用它既可裁剪输出页面内容,又可用程序代码直接实现 复杂打印. ...

  8. sql先分组,再算百分比

    --先分组,再算百分比 SELECT  a.CooperationIntention ,         a.OrganizationID ,         COUNT(*) 数量 , CONVER ...

  9. Spring MVC入门(一)—— SpringMVC的执行流程与常用注解

    一.什么是SpringMVC SpringMVC就是类似于Struts2的mvc框架,属于SpringFrameWork的后续产品.在模型层中与视图层的交互部分. springMVC执行流程: 二.常 ...

  10. opencv3.2.0形态学滤波之腐蚀

    /* 腐蚀(erode)含义: 腐蚀和膨胀是相反的一对操作,所以腐蚀就是求局部最小值的操作,腐蚀操作使原图中 国的高亮部分被腐蚀,效果图比原图有更小的高亮的区域. 腐蚀函数原型API及参数同膨胀相同 ...