About GallopGallop是一個(gè)功能強(qiáng)大诚欠、性能優(yōu)秀的圖文混排框架顽染。
Features
主要用于解決以下需求:
滾動(dòng)列表的性能優(yōu)化。Gallop使用異步繪制轰绵、視圖層級(jí)合并粉寞、觀察mainRunloop、對(duì)布局模型預(yù)先緩存等方法左腔,能在實(shí)現(xiàn)復(fù)雜的圖文混排界面時(shí)唧垦,仍然保持一個(gè)相當(dāng)優(yōu)秀的滾動(dòng)性能(FPS基本保持在60)。
項(xiàng)目內(nèi)有使用Gallop構(gòu)建的微信朋友圈Demo
實(shí)現(xiàn)圖文混排界面液样,比如在文本中添加表情振亮,對(duì)文字添加點(diǎn)擊鏈接。Gallop還提供了方便的方法可以直接完成表情鞭莽、URL鏈接坊秸、@用戶、#話題#等的解析澎怒。
簡便地實(shí)現(xiàn)對(duì)網(wǎng)絡(luò)圖片和本地圖片的圓角和模糊處理褒搔,并能提供緩存,無需重復(fù)處理,優(yōu)化性能星瘾。
方便的解析HTML渲染生成原生iOS頁面走孽。項(xiàng)目內(nèi)有使用Gallop構(gòu)建的知乎日?qǐng)?bào)Demo
滾動(dòng)性能請(qǐng)使用真機(jī)調(diào)試查看效果
Demo Snapshot
Modifications
v0.3.5
LWImageStorage 新增一個(gè)屬性isBlur。本地圖片時(shí)琳状,將在子線程進(jìn)行模糊處理磕瓷;當(dāng)網(wǎng)絡(luò)圖片時(shí),將在子線程進(jìn)行模糊處理并直接緩存模糊的版本念逞。詳見Demo困食。
v0.3.4
支持CoreData來緩存布局模型
v0.3.3
更改了集成方式,解決了與SDWebImage部分文件沖突的問題肮柜。
v0.3.2
現(xiàn)在陷舅,設(shè)置了圓角半徑的網(wǎng)絡(luò)圖片將額外緩存一份倒彰,解決了內(nèi)存消耗過大的問題审洞。
v0.3.1
解析HTML渲染生成原生iOS頁面時(shí),圖片可以按照原圖比例自適應(yīng)高度了待讳。
v0.3.0
增加了解析HTML渲染生成原生iOS頁面的功能芒澜。
v0.2.5
對(duì)圖片加載進(jìn)行了優(yōu)化。
v0.2.4
增加了TransactionGroup创淡,LWTransaction痴晦,CALayer+LWTransaction。
v0.2.3
文字添加了描邊繪制模式琳彩。
v0.2.2
增加了一個(gè)方法
- (void)lw_addLinkForWholeTextStorageWithData:(id)data linkColor:(UIColor *)linkColor highLightColor:(UIColor *)highLightColor;
?
廢棄了方法
- (id)initWithFrame:(CGRect)frame maxImageStorageCount:(NSInteger)maxCount;
?
現(xiàn)在誊酌,LWAsyncDisplayView內(nèi)部將自動(dòng)維護(hù)一個(gè)復(fù)用池,可以為LWStorage設(shè)置一個(gè)NSString*類型的Identifier露乏,來復(fù)用內(nèi)部的相關(guān)UIView,簡化API碧浊。
修復(fù)了對(duì)文字添加鏈接重疊而發(fā)生沖突的bug.
TODO
對(duì)視頻、音頻的支持瘟仿。
Requirements
使用Gallop實(shí)現(xiàn)網(wǎng)絡(luò)圖片加載部分依賴于SDWebImage 'SDWebImage', '~>3.7'
HTML解析依賴libxml2庫
Installation
在XCode的Build Phases-> Link Binary With Libraries中添加libxml2.tbd庫
在XCode的Build Setting->Header Search Paths中添加‘/usr/include/libxml2’
安裝SDWebImage
將Gallop文件夾下的.h及.m文件添加到你的工程當(dāng)中
#import "Gallop.h"
Usage
API
Quickstart
#import "Gallop.h"
1.生成一個(gè)文本模型
LWTextStorage* textStorage = [[LWTextStorage alloc] init];
textStorage.text = @"waynezxcv";
textStorage.font = [UIFont systemFontOfSize:15.0f];
textStorage.textColor = RGB(113, 129, 161, 1);
/*** 為文本添加點(diǎn)擊鏈接事件 ***/
[textStorage addLinkWithData:data
inRange:NSMakeRange(0,statusModel.name.length)
linkColor:RGB(113, 129, 161, 1)
highLightColor:RGB(0, 0, 0, 0.15)];
/*** 點(diǎn)擊鏈接回調(diào) ***/
- (void)lwAsyncDisplayView:(LWAsyncDisplayView *)asyncDisplayView didCilickedLinkWithfData:(id)data;
/*** 用本地圖片替換掉指定位置的文字 ***/
[textStorage lw_replaceTextWithImage:[UIImage imageNamed:@"img"]
contentMode:UIViewContentModeScaleAspectFill
imageSize:CGSizeMake(60, 60)
alignment:LWTextAttachAlignmentTop
range:NSMakeRange(webImageTextStorage.text.length - 7, 0)];
/*** 用網(wǎng)絡(luò)圖片替換掉指定位置的文字 ***/
[textStorage lw_replaceTextWithImageURL:[NSURL URLWithString:@"https://avatars0.githubusercontent.com/u/8408918?v=3&s=460"]
contentMode:UIViewContentModeScaleAspectFill
imageSize:CGSizeMake(60, 60)
alignment:LWTextAttachAlignmentTop
range:NSMakeRange(webImageTextStorage.text.length - 7, 0)];
/*** 用UIView替換掉指定位置的文字 ***/
[textStorage lw_replaceTextWithView:[[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 60.0f, 30.0f)]
contentMode:UIViewContentModeScaleAspectFill
size:CGSizeMake(60.0f, 30.0f)
alignment:LWTextAttachAlignmentTop
range:NSMakeRange(1,0)];
?
2.生成一個(gè)圖片模型
/*** 本地圖片 ***/
LWImageStorage* imamgeStorage = [[LWImageStorage alloc] init];
imamgeStorage.contents = [UIImage imageNamed:@"pic.jpeg"];
imamgeStorage.frame = CGRectMake(textStorage.left, textStorage.bottom + 20.0f, 80, 80);
imamgeStorage.cornerRadius = 40.0f;//設(shè)置圓角半徑
/*** 網(wǎng)絡(luò)圖片 ***/
LWImageStorage* imamgeStorage = [[LWImageStorage alloc] init];
imamgeStorage.contents = [NSURL URLWithString:@"https://avatars0.githubusercontent.com/u/8408918?v=3&s=460"];
imamgeStorage.frame = CGRectMake(textStorage.left, textStorage.bottom + 20.0f, 80, 80);
imamgeStorage.cornerRadius = 40.0f;
/*** 點(diǎn)擊圖片回調(diào) ***/
- (void)lwAsyncDisplayView:(LWAsyncDisplayView *)asyncDisplayView didCilickedImageStorage:(LWImageStorage *)imageStorage touch:(UITouch *)touch;
?
3.生成布局模型
LWLayout* layout = [[LWLayout alloc] init];
/*** 將LWstorage實(shí)例添加到layout當(dāng)中 ***/
[layout addStorage:textStorage];
[layout addStorage:imamgeStorage];
?
4.創(chuàng)建LWAsyncDisplayView箱锐,并將LWLayout實(shí)例賦值給創(chuàng)建LWAsyncDisplayView對(duì)象
LWAsyncDisplayView* asyncDisplayView = [[LWAsyncDisplayView alloc] initWithFrame:CGRectZero];
asyncDisplayView.layout = layout;
[self.view addSubview:asyncDisplayView];
?
5.解析HTML生成iOS原生頁面
/*** 創(chuàng)建LWHTMLDisplayView ***/
LWHTMLDisplayView* htmlView = [[LWHTMLDisplayView alloc] initWithFrame:self.view.bounds];
htmlView.parentVC = self;
htmlView.displayDelegate = self;
[self.view addSubview:htmlView];
/*** 獲取LWStorageBuilder ***/
LWStorageBuilder* builder = htmlView.storageBuilder;
/*** 創(chuàng)建LWLayout ***/
LWLayout* layout = [[LWLayout alloc] init];
/*** 創(chuàng)建LWHTMLTextConfig ***/
LWHTMLTextConfig* contentConfig = [[LWHTMLTextConfig alloc] init];
contentConfig.font = [UIFont fontWithName:@"Heiti SC" size:15.0f];
contentConfig.textColor = RGB(50, 50, 50, 1);
contentConfig.linkColor = RGB(232, 104, 96,1.0f);
contentConfig.linkHighlightColor = RGB(0, 0, 0, 0.35f);
/*** 創(chuàng)建另一個(gè)LWHTMLTextConfig ***/
LWHTMLTextConfig* strongConfig = [[LWHTMLTextConfig alloc] init];
strongConfig.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:15.0f];
strongConfig.textColor = [UIColor blackColor];
/*** 通過XPath解析HTML并生成LWStorage ***/
/*** 通過UIEdgeInsets設(shè)置布局傳入第二個(gè)參數(shù) ***/
/*** 標(biāo)簽名對(duì)應(yīng)的LWHTMLTextConfig以字典的Key-Value格式傳入最后一個(gè)參數(shù) ***/
[builder createLWStorageWithXPath:@"http://div[@class='content']/p"
edgeInsets:UIEdgeInsetsMake([layout suggestHeightWithBottomMargin:10.0f], 10.0f, 10.0, 10.0f)
configDictionary:@{@"p":contentConfig,
@"strong":strongConfig,
@"em":strongConfig}];
/*** 獲取生成的LWStorage實(shí)例數(shù)組 ***/
NSArray* storages = builder.storages;
/*** 添加到LWLayout實(shí)例 ***/
[layout addStorages:storages];
/*** 給LWHTMLDisplayView對(duì)象并賦值 ***/
htmlView.layout = layout;
?
長按關(guān)注:
QQ群:427763454
歡迎你的投稿,展示的你的技術(shù)文章:812920365@qq.com