GONMarkupParser的使用
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的使用的更多相关文章
- [翻译] GONMarkupParser
GONMarkupParser https://github.com/nicolasgoutaland/GONMarkupParser NSString *inputText = @"Sim ...
- iOS之UI--富文本总结
文章内容大纲 1.NSMutableAttributedString的基本使用 2.NSMutableAttributedString的简易封装 3.使用开源代码GOBMarkupPaser处理富文本 ...
- iOS富文本-NSAttributedString简单封装
直接调用系统的写起来比较麻烦,封装一下 因为要简单所以就写类方法 WJAttributeStyle 基类 ) { ; i < styles.count; i ++) { ...
随机推荐
- SpringBoot入门 (四) 数据库访问之JdbcTemplate
本文记录在SpringBoot中使用JdbcTemplate访问数据库. 一 JDBC回顾 最早是在上学时接触的使用JDBC访问数据库,主要有以下几个步骤: 1 加载驱动 Class.forName( ...
- [笔记] Python字符串
1.字符串是以单引号'或双引号"括起来的任意文本 比如'Mifen',"Amd794",'-956-$*'等等.注意:不能单双引号组合,涉及字符串中存在单双引号出现,应用 ...
- redis实战笔记(4)-第4章 数据安全与性能保障
本章主要内容 4.1 将数据持久化至硬盘 4.2 将数据复制至其他机器 4.3 处理系统故障 4.4 Redis事务 4.5 非事务型流水线( non-transactional pipeline) ...
- lucene源码分析(5)lucence-group
1. 普通查询的用法 org.apache.lucene.search.IndexSearcher public void search(Query query, Collector results) ...
- 触摸UITextView找到该触摸点的文字
参加了一个比赛有一道题是如标题一样,在UITextView上触摸找到该触摸点对应的文字,比赛也可以查资料,当时做的时候就是抱着玩玩的心态试试也没认真做,就没查就去吃饭去了,昨晚下班回去在思考这个问题发 ...
- dev中文本框等获取焦点事件
<ClientSideEvents GotFocus="GotFocus" /> editContract.SetFocus()//设置文本框等的焦点 function ...
- C#中解决Response.AddHeader("Content-Disposition", "attachment; filename=" + filename)下载文件时文件名乱码的问题
问题:下载文件时文件名乱码怎么解决? 在C#写后台代码过程中,经常遇到下载文件出现文件名乱码的问题,在网上找了很多方法,总是存在浏览器不兼容的问题,当IE浏览器不乱码时,火狐浏览器就会乱码,后来经过反 ...
- NSDictionary 和NSArray 排序(sort)
排序: NSMutableDictionary *dic=[[NSMutableDictionary alloc]init]; [dic setValue:@"第3个" forKe ...
- properties配置文件编码问题
properties配置文件编码问题 person.last-name=哈哈 person.age=18 person.bitrh=2019/01/12 person.boss=false perso ...
- POJ3126(KB1-F BFS)
Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief ...