使用富文本OHAttributedLabel
OHAttributedLabel?富文本標(biāo)簽
https://github.com/AliSoftware/OHAttributedLabel
以下是我渲染出來(lái)的效果
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.
這個(gè)類允許你讓UILabel使用富文本,顯示出極具動(dòng)感而獨(dú)特的文本標(biāo)簽.它繼承至UILabel并添加了一個(gè)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.
注意:這個(gè)類兼容iOS4.3+,在iOS6 SDK出來(lái)之前就已經(jīng)出現(xiàn)了(iOS6 SDK出來(lái)后使得UILabel支持了NSAttributedLabel).如果你想兼容早期版本,你還是可以使用這個(gè)類.
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.
這個(gè)類支持高亮鏈接地址,他可以自動(dòng)檢測(cè)你的文本中的鏈接地址,并使得他們可以觸發(fā)觸摸事件.你也可以給你的文本添加觸摸事件.
?
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這個(gè)類,你還會(huì)發(fā)現(xiàn)有NS(Mutable)AttributedString的category,讓你更加便利的創(chuàng)建富文本的相關(guān)屬性.
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這個(gè)類也有個(gè)category,我添加了extendedURL屬性.這個(gè)屬性返回與標(biāo)準(zhǔn)鏈接中的URL同樣的值,并返回了一個(gè)格式化過(guò)的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.
這個(gè)庫(kù)允許你使用tag注釋的方式來(lái)幫助你快速的使用富文本.
- 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 只是一個(gè)幫助你簡(jiǎn)單創(chuàng)建富文本的工具,他可不是一個(gè)真正的HTML標(biāo)簽解析器.
?
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; }?
使用教程:
請(qǐng)?jiān)贏RC下使用,不要ARC與MRC混用造成內(nèi)存泄露!
源碼地址 ? http://pan.baidu.com/s/1pJnY8BL
#import "OHAttributedLabel.h"
#import "OHParagraphStyle.h"
#import "OHTouchesGestureRecognizer.h"
以下是渲染的效果:
?
標(biāo)題展示圖片的代碼
// 注冊(cè)字體REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子體");REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"華康少女字體");// 創(chuàng)建富文本stringNSMutableAttributedString* attrStr = \[NSMutableAttributedString attributedStringWithString:\@"游賢明\合抱之木,生于毫末;九層之合,起于壘土;千里之行,始于足下。 "];// 設(shè)置富文本基本屬性[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)];// 設(shè)置樣式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;// 添加進(jìn)主視圖[self.view addSubview:label];?
更加完美的設(shè)置:
// 注冊(cè)字體REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子體");REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"華康少女字體");// 創(chuàng)建富文本stringNSMutableAttributedString* attrStr = \[NSMutableAttributedString attributedStringWithString:\@"還記得嗎,窗外那被月光染亮的海洋\n你還記得嗎,是愛(ài)讓彼此把夜點(diǎn)亮\n為何后來(lái)我們用沉默取代依賴,曾經(jīng)朗朗星空,漸漸陰霾\n心碎離開,轉(zhuǎn)身回到最初荒涼里等待\n為了寂寞,是否找個(gè)人填心中空白\n我們變成了世上,最熟悉的陌生人\n今后各自曲折,各自悲哀\n只怪我們愛(ài)得那么洶涌,愛(ài)得那么深\n于是夢(mèng)醒了擱淺了沉默了揮手了卻回不了神\n如果當(dāng)初在交會(huì)時(shí)能忍住了,激動(dòng)的靈魂"];// 設(shè)置富文本基本屬性[attrStr setFontName:CUSTOM_FONT(@"華康少女字體", 0) size:12];[attrStr setTextColor:[UIColor whiteColor]];[attrStr setTextColor:[UIColor redColor] range:NSMakeRange(9,7)];// 首字大寫以及顏色設(shè)置[attrStr setFontName:CUSTOM_FONT(@"華康少女字體", 0) size:23 range:NSMakeRange(0,1)];[attrStr setTextColor:[UIColor yellowColor] range:NSMakeRange(0,1)];// 中間字體重新設(shè)定[attrStr setFontName:CUSTOM_FONT(@"新蒂小丸子體", 0) size:17 range:NSMakeRange(92,16)];[attrStr setTextColor:[UIColor cyanColor] range:NSMakeRange(92,16)];// 設(shè)置樣式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來(lái)顯示文章
// 注冊(cè)字體REGISTER_FONT(bundleFont(@"xinDiXiaoWanzi.ttf"), @"新蒂小丸子體");REGISTER_FONT(bundleFont(@"huaKangShaoNv.ttf"), @"華康少女字體");// 創(chuàng)建富文本stringNSMutableAttributedString* attrStr = \[NSMutableAttributedString attributedStringWithString:\@"人啊,再?gòu)?qiáng)大,又怎去敵那一抔黃土呢?說(shuō)到底,這人生也不過(guò)就是山一程,水一程。與其把自己鎖于跌撞里郁怨寡歡、得失不衡,還不如莞然一笑,迎著陽(yáng)光,把所有的疼痛與繁復(fù),都一一踏在腳下拋置身后。再以最安然祥和的顏靨,來(lái)謝過(guò)這一場(chǎng)生。 \n------題記/云微若雨\n是否,在佛堂看僧敲木魚聽梵音鐘聲,于寺廟拈香誦經(jīng)濯骨洗心,虔誠(chéng)脫胎如出家之人,方可讓內(nèi)心,少一份紛擾,多一份恬淡呢?可是,這六界風(fēng)沙本就是那么的強(qiáng)烈與無(wú)常啊,怎去奢求一方安寧?塵世煙火如是冷凜如此淡薄,寄居蕭瑟,飄絮染野,人們能做的,想是唯有撫著這顆澀澀的心,撿拾滿地殘紅,淺淺掠過(guò)吧。"];// 設(shè)置富文本基本屬性[attrStr setFontName:CUSTOM_FONT(@"華康少女字體", 0) size:12];[attrStr setTextColor:[UIColor whiteColor]];// 設(shè)置段落樣式OHParagraphStyle* paragraphStyle = [OHParagraphStyle defaultParagraphStyle];paragraphStyle.textAlignment = kCTTextAlignmentNatural;paragraphStyle.paragraphSpacing = 12.f;paragraphStyle.paragraphSpacingBefore = 10.f;paragraphStyle.lineBreakMode = kCTLineBreakByWordWrapping;paragraphStyle.firstLineHeadIndent = 24.f; // 段落首字縮進(jìn)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;// 添加進(jìn)主視圖[self.view addSubview:label];?
?
?
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/YouXianMing/p/3684621.html
總結(jié)
以上是生活随笔為你收集整理的使用富文本OHAttributedLabel的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 如何定义和区分高级软件开发工程师?
- 下一篇: 下拉导航中绝对定位与相对定位问题