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. 发布Framework 4.0到iis时,出现HTTP 错误 403.14 - Forbidden

    新发布MVC到服务器的时候,经常碰到403.14错误,绝大部分的时候都是因为Framework 4.0需要重新注册下,在运行里输入:C:\Windows\Microsoft.NET\Framework ...

  2. UIKit 框架之UICollectionView

    1.自定义UICollectionViewCell 在myCollectionViewCell.h中声明两个属性 // // myCollectionViewCell.h // UICollectio ...

  3. 结束回调事件(开头必须cp开头,JSProperties传参)

    <dx:ASPxComboBox ID="comBrand" CssClass="case" ClientInstanceName="comBr ...

  4. BG.Hive - part2

    1. 将mysql的订单数据导入hive的分区表(桶.倾斜)[partition,bucket,skew] a> 在Hive中新建分区表 CREATE TABLE IF NOT EXISTS H ...

  5. [C语言] 数据结构-逻辑结构和物理结构

    数据结构:相互之间存在一种或多种特定关系的数据元素的集合 1.数据结构分为逻辑结构和物理结构 集合结构:集合结构中的数据元素除了同属于一个集合外,他们之间没有其他关系 线性结构:线性结构中的数据元素之 ...

  6. [android] 手机卫士黑名单功能(短信拦截)

    前面我们把需要拦截的手机号都存储和展示出来了,接下来是使用广播接收者拦截短信了,这个广播接收者需要和一个服务绑定,服务开启的时候,接收者存在,服务停掉时,接收者关闭 在service包下定义一个类Ca ...

  7. HwUI下载地址

    下载地址:HwUI.0.0.1.zip

  8. Code Signal_练习题_All Longest Strings

    Given an array of strings, return another array containing all of its longest strings. Example For i ...

  9. Android手动显示和隐藏软键盘

    1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) InputMethodManager imm = (InputMethodManager) getSystemService(Contex ...

  10. Math.random理解练习

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...