https://github.com/AliSoftware/OHAttributedLabel

以下是我渲染出来的效果

OHAttributedLabel

This class allows you to use a UILabel with NSAttributedStrings, in order to display styled text with various style (mixed fonts, color, size, ...) in a unique label. It is a subclass of UILabel which adds an attributedText property. Use this property, instead of the text property, to set and get the NSAttributedString to display.

这个类允许你让UILabel使用富文本,显示出极具动感而独特的文本标签.它继承至UILabel并添加了一个attributedText属性.

Note: This class is compatible with iOS4.3+ and has been developped before the release of the iOS6 SDK (before Apple added support for NSAttributedLabel in the UILabel class itself). It can still be used with the iOS6 SDK (the attributedText property hopefully match the one chosen by Apple) if you need support for eariler iOS versions or for the additional features it provides.

注意:这个类兼容iOS4.3+,在iOS6 SDK出来之前就已经出现了(iOS6 SDK出来后使得UILabel支持了NSAttributedLabel).如果你想兼容早期版本,你还是可以使用这个类.

This class also support hyperlinks and URLs. It can automatically detect links in your text, color them and make them touchable; you can also add "custom links" in your text by attaching an URL to a range of your text and thus make it touchable, and even then catch the event of a touch on a link to act as you wish to.

这个类支持高亮链接地址,他可以自动检测你的文本中的链接地址,并使得他们可以触发触摸事件.你也可以给你的文本添加触摸事件.

NSAttributedString and NSTextChecking additions

In addition to this OHAttributedLabel class, you will also find a category of NS(Mutable)AttributedString to ease creation and manipulation of common attributes of NSAttributedString (to easily change the font, style, color, ... of a range of the string). See the header file NSAttributedString+Attributes.h for a list of those comodity methods.

除了OHAttributedLabel这个类,你还会发现有NS(Mutable)AttributedString的category,让你更加便利的创建富文本的相关属性.

Example:

// Build an NSAttributedString easily from a NSString
NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:txt];
// Change font, text color, paragraph style
[attrStr setFont:[UIFont fontWithName:@"Helvetica" size:18]];
[attrStr setTextColor:[UIColor grayColor]];OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];
paragraphStyle.textAlignment = kCTJustifiedTextAlignment;
paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;
paragraphStyle.firstLineHeadIndent = 30.f; // indentation for first line
paragraphStyle.lineSpacing = 3.f; // increase space between lines by 3 points
[attrStr setParagraphStyle:paragraphStyle];// Change the color and bold of only one part of the string
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(10,3)];
[attrStr setTextBold:YES range:NSMakeRange(10,8)];// Add a link to a given portion of the string
[attrStr setLink:someNSURL range:NSMakeRange(8,20)];

There is also a category for NSTextCheckingResult that adds the extendedURL property. This property returns the same value as the URL value for standard link cases, and return a formatted Maps URL for NSTextCheckingTypeAddress link types, that will open Google Maps in iOS version before 6.0 and the Apple's Maps application in iOS 6.0 and later.

NSTextCheckingResult这个类也有个category,我添加了extendedURL属性.这个属性返回与标准链接中的URL同样的值,并返回了一个格式化过的NSTextCheckingTypeAddress链接类型.

OHASMarkupParsers and simple markup to build your attributed strings easily

The library also comes with very simple tag parsers to help you build NSAttributedStrings easily using very simple tags.

这个库允许你使用tag注释的方式来帮助你快速的使用富文本.

  • the class OHASBasicHTMLParser can parse simple HTML tags like <b> and <u> to make bold and underlined text, change the font color using <font color='…'>, etc
  • the class OHASBasicMarkupParser can parse simple markup like *bold text*, _underlined text_ and change the font color using markup like {red|some red text} or {#ff6600|Yeah}.

    // Example 1: parse HTML in attributed string
    basicMarkupLabel.attributedText = [OHASBasicHTMLParser attributedStringByProcessingMarkupInAttributedString:basicMarkupLabel.attributedText];// Example 2: parse basic markup in string
    NSAttributedString* as = [OHASBasicMarkupParser attributedStringByProcessingMarkupInString:@"Hello *you*!"];// Example 3: //process markup in-place in a mutable attributed string
    NSMutableAttributedString* mas = [NSMutableAttributedString attributedStringWithString:@"Hello *you*!"];
    [OHASBasicMarkupParser processMarkupInAttributedString:mas];
    

Note that OHASBasicHTMLParser is intended to be a very simple tool only to help you build attributed string easier: this is not intended to be a real and complete HTML interpreter, and will never be. For improvements of this feature, like adding other tags or markup languages, refer to issue #88)

注意,OHASBasicHTMLParser 只是一个帮助你简单创建富文本的工具,他可不是一个真正的HTML标签解析器.

UIAppearance support

The OHAttributedLabel class support the UIAppearance proxy API (available since iOS5). See selectors and properties marked using the UI_APPEARANCE_SELECTOR in the header.

This means that if you are targetting iOS5, you can customize all of your OHAttributedLabel links color and underline style to fit your application design, only in one call at the beginning of your application, instead of having to customize these for each instance.

For example, your could implement this in your application:didFinishLoadingWithOptions: delegate method to make all your OHAttributedLabel instances in your whole app display links in green and without underline instead of the default underlined blue:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{[ [OHAttributedLabel appearance] setLinkColor:[UIColor colorWithRed:0.0 green:0.4 blue:0.0 alpha:1.0] ];[ [OHAttributedLabel appearance] setLinkUnderlineStyle:kCTUnderlineStyleNone ];return YES;
}

使用教程:

请在ARC下使用,不要ARC与MRC混用造成内存泄露!

源码地址   http://pan.baidu.com/s/1pJnY8BL

#import "OHAttributedLabel.h"
#import "OHParagraphStyle.h"
#import "OHTouchesGestureRecognizer.h"

    // 注册字体REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子体");REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"华康少女字体");// 创建富文本stringNSMutableAttributedString* attrStr = \[NSMutableAttributedString attributedStringWithString:\@"还记得吗,窗外那被月光染亮的海洋\你还记得吗,是爱让彼此把夜点亮\为何后来我们用沉默取代依赖 曾经朗朗星空,渐渐阴霾\心碎离开,转身回到最初荒凉里等待 为了寂寞,是否找个人填心中空白\我们变成了世上,最熟悉的陌生人 今后各自曲折,各自悲哀\只怪我们爱得那么汹涌,爱得那么深 于是梦醒了搁浅了沉默了挥手了\却回不了神,如果当初在交会时能忍住了 激动的灵魂"];// 设置富文本基本属性[attrStr setFont:[UIFont fontWithName:CUSTOM_FONT(@"华康少女字体", 0) size:12]];[attrStr setTextColor:[UIColor whiteColor]];[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(9,4)];[attrStr setTextIsUnderlined:YES range:NSMakeRange(9, 4)];[attrStr setTextColor:[UIColor greenColor] range:NSMakeRange(125,30)];[attrStr setFontName:CUSTOM_FONT(@"新蒂小丸子体", 0) size:12 range:NSMakeRange(125,30)];// 设置样式OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];paragraphStyle.textAlignment = kCTTextAlignmentLeft;paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;paragraphStyle.firstLineHeadIndent = 0.f; // indentation for first lineparagraphStyle.lineSpacing = 3.f; // increase space between lines by 3 points[attrStr setParagraphStyle:paragraphStyle];// 初始化富文本labelOHAttributedLabel *label = \[[OHAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];label.attributedText = attrStr;label.center = self.view.center;// 添加进主视图[self.view addSubview:label];

以下是渲染的效果:

标题展示图片的代码

    // 注册字体REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子体");REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"华康少女字体");// 创建富文本stringNSMutableAttributedString* attrStr = \[NSMutableAttributedString attributedStringWithString:\@"游贤明\合抱之木,生于毫末;九层之合,起于垒土;千里之行,始于足下。 "];// 设置富文本基本属性[attrStr setFont:[UIFont fontWithName:CUSTOM_FONT(@"华康少女字体", 0) size:12]];[attrStr setTextColor:[UIColor whiteColor]];[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(0,3)];[attrStr setFontName:CUSTOM_FONT(@"新蒂小丸子体", 0) size:26 range:NSMakeRange(0,3)];// 设置样式OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];paragraphStyle.textAlignment = kCTTextAlignmentLeft;paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;paragraphStyle.firstLineHeadIndent = 0.f; // indentation for first lineparagraphStyle.lineSpacing = 3.f; // increase space between lines by 3 points[attrStr setParagraphStyle:paragraphStyle];// 初始化富文本labelOHAttributedLabel *label = \[[OHAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];label.attributedText = attrStr;label.center = self.view.center;// 添加进主视图[self.view addSubview:label];

更加完美的设置:

    // 注册字体REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子体");REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"华康少女字体");// 创建富文本stringNSMutableAttributedString* attrStr = \[NSMutableAttributedString attributedStringWithString:\@"还记得吗,窗外那被月光染亮的海洋\n你还记得吗,是爱让彼此把夜点亮\n为何后来我们用沉默取代依赖,曾经朗朗星空,渐渐阴霾\n心碎离开,转身回到最初荒凉里等待\n为了寂寞,是否找个人填心中空白\n我们变成了世上,最熟悉的陌生人\n今后各自曲折,各自悲哀\n只怪我们爱得那么汹涌,爱得那么深\n于是梦醒了搁浅了沉默了挥手了却回不了神\n如果当初在交会时能忍住了,激动的灵魂"];// 设置富文本基本属性[attrStr setFontName:CUSTOM_FONT(@"华康少女字体", 0) size:12];[attrStr setTextColor:[UIColor whiteColor]];[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(9,7)];// 首字大写以及颜色设置[attrStr setFontName:CUSTOM_FONT(@"华康少女字体", 0) size:23 range:NSMakeRange(0,1)];[attrStr setTextColor:[UIColor yellowColor] range:NSMakeRange(0,1)];// 中间字体重新设定[attrStr setFontName:CUSTOM_FONT(@"新蒂小丸子体", 0) size:17 range:NSMakeRange(92,16)];[attrStr setTextColor:[UIColor cyanColor] range:NSMakeRange(92,16)];// 设置样式OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];paragraphStyle.textAlignment = kCTTextAlignmentCenter;paragraphStyle.paragraphSpacing = 3.f;paragraphStyle.paragraphSpacingBefore = 10.f;paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;paragraphStyle.firstLineHeadIndent = 0.f; // indentation for first lineparagraphStyle.lineSpacing = 3.f; // increase space between lines by 3 points[attrStr setParagraphStyle:paragraphStyle];// 初始化富文本labelOHAttributedLabel *label = \[[OHAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];label.attributedText = attrStr;label.center = self.view.center;

用富文本Label来显示文章

    // 注册字体REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子体");REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"华康少女字体");// 创建富文本stringNSMutableAttributedString* attrStr = \[NSMutableAttributedString attributedStringWithString:\@"人啊,再强大,又怎去敌那一抔黄土呢?说到底,这人生也不过就是山一程,水一程。与其把自己锁于跌撞里郁怨寡欢、得失不衡,还不如莞然一笑,迎着阳光,把所有的疼痛与繁复,都一一踏在脚下抛置身后。再以最安然祥和的颜靥,来谢过这一场生。 \n------题记/云微若雨\n是否,在佛堂看僧敲木鱼听梵音钟声,于寺庙拈香诵经濯骨洗心,虔诚脱胎如出家之人,方可让内心,少一份纷扰,多一份恬淡呢?可是,这六界风沙本就是那么的强烈与无常啊,怎去奢求一方安宁?尘世烟火如是冷凛如此淡薄,寄居萧瑟,飘絮染野,人们能做的,想是唯有抚著这颗涩涩的心,捡拾满地残红,浅浅掠过吧。"];// 设置富文本基本属性[attrStr setFontName:CUSTOM_FONT(@"华康少女字体", 0) size:12];[attrStr setTextColor:[UIColor whiteColor]];// 设置段落样式OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];paragraphStyle.textAlignment = kCTTextAlignmentNatural;paragraphStyle.paragraphSpacing = 12.f;paragraphStyle.paragraphSpacingBefore = 10.f;paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;paragraphStyle.firstLineHeadIndent = 24.f;  // 段落首字缩进paragraphStyle.lineSpacing = 4.f;           // 段落中两行的行间距[attrStr setParagraphStyle:paragraphStyle];// 初始化富文本labelOHAttributedLabel *label = \[[OHAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];label.attributedText = attrStr;label.center = self.view.center;// 添加进主视图[self.view addSubview:label];

OHAttributedLabel 富文本详解相关推荐

  1. linux隔行打印文本,详解正则表达式及Linux三大文本处理工具

    grep.sed和awk都是文本处理工具,虽然都是文本处理工具但却都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的,否则也不会出现三个文本处理命令了. 一.正则表达式 1.匹配字符的类型 [ ...

  2. ios开发text kit_IOS开发入门之TextKit详解

    本文将带你了解IOS开发入门iOS 开发 富文本详解之TextKit详解,希望本文对大家学IOS有所帮助. textkit结构 textkit使用步骤 #Mark - 1. 自定义label  --c ...

  3. MVC 使用 Ueditor富文本编辑器

    一.Ueditor 1.下载Ueditor富文本编辑器 官方下载地址: http://ueditor.baidu.com/website/download.html 建议下载开发版,此处我下载的是 . ...

  4. iOS富文本字符串AttributedString详解

    原文链接: iOS富文本字符串AttributedString详解 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://gi ...

  5. 微信小程序富文本解析点击图片放大_微信小程序解析富文本过程详解

    前言 最近公司在开发OTA微信小程序,一些页面的详情内容是HTML富文本格式的的,但是微信小程序不能直接解析HTML,需要将内容中的HTML标签转换成微信小程序所支持的标签. 开始的时候想过自己写方法 ...

  6. 微信小程序富文本rich-text使用详解-微信小程序系统学习攻略​

    也许你迷茫,但是我想说,在你迷茫的同时,保持本心,过好今天就好. 在微信小程序开发中,rich-text 标签可以用来显示富文本 html内容,可以渲染部分html标签,全局支持class和style ...

  7. 【愚公系列】2022年08月 微信小程序-(rich-text)富文本和(text)文本的详解

    文章目录 前言 一.富文本使用 1.富文本的基本使用 2.富文本中预览图片 2.1 原生使用 2.2 组件封装使用 2.2.1 组件封装 2.2.2 组件使用 二.文本 1.文本属性 前言 富文本格式 ...

  8. RichEditeText——android图文混排富文本文章编辑器实现详解

    需求:android 实现富文本编辑器,并且实现html解析和生成. 功能点: 字体加粗,斜体,下划线,删除线 字体设置大小   默认大(18px),中(16px),小(14px) 字体设置颜色 换行 ...

  9. ios开发 html编辑器,iOS实现富文本编辑器的方法详解

    前言 富文本编辑器不同于文本编辑器,国内做的比较好的比如有百度的UEditor和kindEditor.但是这两个也有它的缺点:界面过于复杂.不够简洁.UI设计也比较落后.不够轻量化,这篇文章我们将给大 ...

最新文章

  1. Elasticsearch性能优化实战指南
  2. 中蒙最大陆路口岸2018年运行中欧班列1052列
  3. DGA短域名(360样本) mark下 下次分析可以参考
  4. JavaScript中Console的9个常用调试命令
  5. C语言高级编程:char、signed char 和 unsigned char的使用区别
  6. 微海鼠标自动点击器 支持录制和循环播放
  7. 【毕业设计】JAVA文件压缩与解压缩实践(源代码+论文)
  8. 基于RFM模型的用户价值的数据分析报告
  9. 2018圆通科技面试题目
  10. 【汤家凤考研数学手稿】第五章 定积分【重点】
  11. CCF计算机职业资格认证数字排序问题
  12. 电子书格式转换方法集锦
  13. 企业常见的5种经济活动形式用借贷记账法在资产负债表上的体现
  14. ps scavenge java_JVM源码分析(四)Parralel Scavenge 收集器工作流程
  15. UI5-文档-4.5-Controllers
  16. 汇编语言程序设计期末总结
  17. NDIS小端口驱动ndisEdge学习二——小端口驱动的初始化
  18. 什么是RTK?RTK的原理是什么?可以应用在哪些领域
  19. pyecharts制作地图Map
  20. 插画构图的六种基本形式,小白必看!

热门文章

  1. 1_输出n的1-5次方
  2. 航空航天线束测试--Aigtek线束测试仪
  3. 养老院管理系统(Java毕业设计)
  4. Mybatis报错:org.apache.ibatis.binding.BindingException:has an unsupported return type:
  5. Vue手机号正则匹配,姓名加密展示
  6. 良精cms php版漏洞,Phpcms 2008 漏洞整理
  7. ijkplayer音频解码播放架构分析
  8. 世嘉MD游戏开发【八】:淡入和淡出
  9. 图片文字转换成文本怎么做?掌握这些小技巧就够了
  10. 远程桌面报错oracle,【windows】远程桌面报错:由于CredSSP加密Oracle修正