使用富文本OHAttributedLabel
OHAttributedLabel 富文本标签
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 theUILabel
class itself). It can still be used with the iOS6 SDK (theattributedText
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"), @"华康少女字体"); // 创建富文本string
NSMutableAttributedString* attrStr = \
[NSMutableAttributedString attributedStringWithString:\
@"还记得吗,窗外那被月光染亮的海洋\
你还记得吗,是爱让彼此把夜点亮\
为何后来我们用沉默取代依赖 曾经朗朗星空,渐渐阴霾\
心碎离开,转身回到最初荒凉里等待 为了寂寞,是否找个人填心中空白\
我们变成了世上,最熟悉的陌生人 今后各自曲折,各自悲哀\
只怪我们爱得那么汹涌,爱得那么深 于是梦醒了搁浅了沉默了挥手了\
却回不了神,如果当初在交会时能忍住了 激动的灵魂"]; // 设置富文本基本属性
[attrStr setFont:[UIFont fontWithName:CUSTOM_FONT(@"华康少女字体", ) size:]];
[attrStr setTextColor:[UIColor whiteColor]];
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(,)];
[attrStr setTextIsUnderlined:YES range:NSMakeRange(, )];
[attrStr setTextColor:[UIColor greenColor] range:NSMakeRange(,)];
[attrStr setFontName:CUSTOM_FONT(@"新蒂小丸子体", ) size: range:NSMakeRange(,)]; // 设置样式
OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];
paragraphStyle.textAlignment = kCTTextAlignmentLeft;
paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;
paragraphStyle.firstLineHeadIndent = .f; // indentation for first line
paragraphStyle.lineSpacing = .f; // increase space between lines by 3 points
[attrStr setParagraphStyle:paragraphStyle]; // 初始化富文本label
OHAttributedLabel *label = \
[[OHAttributedLabel alloc] initWithFrame:CGRectMake(, , , )];
label.attributedText = attrStr;
label.center = self.view.center; // 添加进主视图
[self.view addSubview:label];
以下是渲染的效果:
标题展示图片的代码
// 注册字体
REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子体");
REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"华康少女字体"); // 创建富文本string
NSMutableAttributedString* attrStr = \
[NSMutableAttributedString attributedStringWithString:\
@"游贤明\
合抱之木,生于毫末;九层之合,起于垒土;千里之行,始于足下。 "]; // 设置富文本基本属性
[attrStr setFont:[UIFont fontWithName:CUSTOM_FONT(@"华康少女字体", ) size:]];
[attrStr setTextColor:[UIColor whiteColor]];
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(,)];
[attrStr setFontName:CUSTOM_FONT(@"新蒂小丸子体", ) size: range:NSMakeRange(,)]; // 设置样式
OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];
paragraphStyle.textAlignment = kCTTextAlignmentLeft;
paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;
paragraphStyle.firstLineHeadIndent = .f; // indentation for first line
paragraphStyle.lineSpacing = .f; // increase space between lines by 3 points
[attrStr setParagraphStyle:paragraphStyle]; // 初始化富文本label
OHAttributedLabel *label = \
[[OHAttributedLabel alloc] initWithFrame:CGRectMake(, , , )];
label.attributedText = attrStr;
label.center = self.view.center; // 添加进主视图
[self.view addSubview:label];
更加完美的设置:
// 注册字体
REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子体");
REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"华康少女字体"); // 创建富文本string
NSMutableAttributedString* attrStr = \
[NSMutableAttributedString attributedStringWithString:\
@"还记得吗,窗外那被月光染亮的海洋\n你还记得吗,是爱让彼此把夜点亮\n为何后来我们用沉默取代依赖,曾经朗朗星空,渐渐阴霾\n心碎离开,转身回到最初荒凉里等待\n为了寂寞,是否找个人填心中空白\n我们变成了世上,最熟悉的陌生人\n今后各自曲折,各自悲哀\n只怪我们爱得那么汹涌,爱得那么深\n于是梦醒了搁浅了沉默了挥手了却回不了神\n如果当初在交会时能忍住了,激动的灵魂"]; // 设置富文本基本属性
[attrStr setFontName:CUSTOM_FONT(@"华康少女字体", ) size:];
[attrStr setTextColor:[UIColor whiteColor]];
[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(,)]; // 首字大写以及颜色设置
[attrStr setFontName:CUSTOM_FONT(@"华康少女字体", ) size: range:NSMakeRange(,)];
[attrStr setTextColor:[UIColor yellowColor] range:NSMakeRange(,)]; // 中间字体重新设定
[attrStr setFontName:CUSTOM_FONT(@"新蒂小丸子体", ) size: range:NSMakeRange(,)];
[attrStr setTextColor:[UIColor cyanColor] range:NSMakeRange(,)]; // 设置样式
OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];
paragraphStyle.textAlignment = kCTTextAlignmentCenter;
paragraphStyle.paragraphSpacing = .f;
paragraphStyle.paragraphSpacingBefore = .f;
paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;
paragraphStyle.firstLineHeadIndent = .f; // indentation for first line
paragraphStyle.lineSpacing = .f; // increase space between lines by 3 points
[attrStr setParagraphStyle:paragraphStyle]; // 初始化富文本label
OHAttributedLabel *label = \
[[OHAttributedLabel alloc] initWithFrame:CGRectMake(, , , )];
label.attributedText = attrStr;
label.center = self.view.center;
用富文本Label来显示文章
// 注册字体
REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子体");
REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"华康少女字体"); // 创建富文本string
NSMutableAttributedString* attrStr = \
[NSMutableAttributedString attributedStringWithString:\
@"人啊,再强大,又怎去敌那一抔黄土呢?说到底,这人生也不过就是山一程,水一程。与其把自己锁于跌撞里郁怨寡欢、得失不衡,还不如莞然一笑,迎着阳光,把所有的疼痛与繁复,都一一踏在脚下抛置身后。再以最安然祥和的颜靥,来谢过这一场生。 \n------题记/云微若雨\n是否,在佛堂看僧敲木鱼听梵音钟声,于寺庙拈香诵经濯骨洗心,虔诚脱胎如出家之人,方可让内心,少一份纷扰,多一份恬淡呢?可是,这六界风沙本就是那么的强烈与无常啊,怎去奢求一方安宁?尘世烟火如是冷凛如此淡薄,寄居萧瑟,飘絮染野,人们能做的,想是唯有抚著这颗涩涩的心,捡拾满地残红,浅浅掠过吧。"]; // 设置富文本基本属性
[attrStr setFontName:CUSTOM_FONT(@"华康少女字体", ) size:];
[attrStr setTextColor:[UIColor whiteColor]]; // 设置段落样式
OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];
paragraphStyle.textAlignment = kCTTextAlignmentNatural;
paragraphStyle.paragraphSpacing = .f;
paragraphStyle.paragraphSpacingBefore = .f;
paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;
paragraphStyle.firstLineHeadIndent = .f; // 段落首字缩进
paragraphStyle.lineSpacing = .f; // 段落中两行的行间距
[attrStr setParagraphStyle:paragraphStyle]; // 初始化富文本label
OHAttributedLabel *label = \
[[OHAttributedLabel alloc] initWithFrame:CGRectMake(, , , )];
label.attributedText = attrStr;
label.center = self.view.center; // 添加进主视图
[self.view addSubview:label];
使用富文本OHAttributedLabel的更多相关文章
- iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片
Label借助富文本显示图片 1.即时通讯项目中语音消息UI的实现,样式如图: 借助富文本在UILabel中显示图片和文字 // 1.创建一个可变的富文本 NSMutableAttributedStr ...
- 在微信小程序中使用富文本转化插件wxParse
在微信小程序中我们往往需要展示一些丰富的页面内容,包括图片.文本等,基本上要求能够解析常规的HTML最好,由于微信的视图标签和HTML标签不一样,但是也有相对应的关系,因此有人把HTML转换做成了一个 ...
- Django中使用富文本编辑器Uedit
Uedit是百度一款非常好用的富文本编辑器 一.安装及基本配置 官方GitHub(有详细的安装使用教程):https://github.com/zhangfisher/DjangoUeditor 1. ...
- django-应用中和amdin使用富文本编辑器kindeditor
文章描述.新闻详情和产品介绍等,都需要大量的文字描述信息或图片.视频.文字的编辑等,这个时候我们就需要介绍第三方富文本编辑器. 今天介绍的是django中绑定和应用kindeditor编辑器: 效果如 ...
- Django使用富文本编辑器
1.下载kindeditor 网址:http://kindeditor.net/demo.php2.解压到项目中 地址:\static\js\kindeditor-4.1.103.删除没用的文件 例如 ...
- [Xcode 实际操作]九、实用进阶-(14)使用富文本CoreText框架创建丰富多彩的文本
目录:[Swift]Xcode实际操作 本文将演示如何使用富文本CoreText框架创建丰富多彩的文本图形. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] imp ...
- Django实现的博客系统中使用富文本编辑器ckeditor
操作系统为OS X 10.9.2,Django为1.6.5. 1.下载和安装 1.1 安装 ckeditor 下载地址 https://github.com/shaunsephton/django-c ...
- 使用富文本编辑器Kindeditor
今天在做需求的时候,遇到有一个字段,需要保存带有格式的内容,决定使用富文本框编辑器Kindeditor来实现,解决方法如下: 登录官网下载控件包: http://kindeditor.net/down ...
- Django后台管理admin或者adminx中使用富文本编辑器
在admin或者adminx后台中使用富文本编辑器 一.建立模型:(安装django-tinymce==2.6.0) from django.db import models from tinymce ...
随机推荐
- hdu 5839(三维几何)
Special Tetrahedron Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- 冒泡法的算法最佳情况下的时间复杂度为什么是O(n)
我在许多书本上看到冒泡排序的最佳时间复杂度是O(n),即是在序列本来就是正序的情况下. 但我一直不明白这是怎么算出来的,因此通过阅读<算法导论-第2版>的2.2节,使用对插入排序最佳时间复 ...
- python itertools模块练习
参考 <python标准库> 也可以参考Vamei博客 列表用着很舒服,但迭代器不需要将所有数据同时存储在内存中. 本章练习一下python 标准库中itertools模块 合并 和 分解 ...
- JavaScript与C#互通的DES加解密算法
原文地址:传送门 本文提供了一个能使JavaScript与C#互通的DES加解密算法的实现,在前台页面中用JavaScript版本的DES算法将数据加密之后,传到服务器端,在服务器端可用C#版本的DE ...
- spring_150905_sqlmapclient
添加ibatis相关的jar包! 实体类: package com.spring.model; public class DogPet { private int id; private String ...
- 2017-2018-1 20179202《Linux内核原理与分析》第二周作业
本周着重学习了汇编指令,并通过反汇编C程序了解栈帧变化. 实践 看了孟老师的演示视频后,我重新写了C程序,如下: int main() { int a=1,b=2; return g(a,b); } ...
- JSP之登录验证码
1.JSP页面中设置输入选项和验证码 <form action=login.do" method="post" > <div class="l ...
- [webpack] devtool里的7种SourceMap[转]
modle: development cheap-source-map debug 不太方便,不是以原来的文件的形式cheap-module-source-map 可以 debugcheap-modu ...
- 【贪心+Treap】BZOJ1691-[Usaco2007 Dec]挑剔的美食家
[题目大意] 有n头奶牛m种牧草,每种牧草有它的价格和鲜嫩度.每头奶牛要求它的牧草的鲜嫩度要不低于一个值,价格也不低于一个值.每种牧草只会被一头牛选择.问最少要多少钱? [思路] 显然的贪心,把奶牛和 ...
- 快速排序-C
#include <stdio.h> #include <stdlib.h> #define N 6 int partition(int arr[], int low, int ...