iOS整個(gè)APP實(shí)現(xiàn)灰色主題的示例代碼
灰色主題
背景
在一些哀悼日,清明節(jié)的時(shí)候app會(huì)實(shí)現(xiàn)一些灰色主題功能,部分app需求是tab首頁(yè)實(shí)現(xiàn)灰色模式就可以,但一些需求是直接整個(gè)app都變?yōu)榛疑!?/p>
- 普通UI界面
- web頁(yè)面
- xib界面
- attributeText加載的htmlString頁(yè)面
- attachment掛件頁(yè)面
實(shí)現(xiàn)方式
基本的實(shí)現(xiàn)方式1,普通頁(yè)面用hook顏色方式2.web頁(yè)面用注入灰色js實(shí)現(xiàn)方式
圖片變灰
重新繪制圖片變?yōu)榛疑a實(shí)現(xiàn)
//image類(lèi)別
- (UIImage *)getGrayImage {
const int RED =1;
const int GREEN =2;
const int BLUE =3;
UIImage *image = self;
// Create image rectangle with current image width/height
CGRect imageRect = CGRectMake(0,0, image.size.width* image.scale, image.size.height* image.scale);
int width = imageRect.size.width;
int height = imageRect.size.height;
// the pixels will be painted to this array
uint32_t *pixels = (uint32_t*) malloc(width * height *sizeof(uint32_t));
// clear the pixels so any transparency is preserved
memset(pixels,0, width * height *sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(pixels, width, height,8, width *sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
// paint the bitmap to our context which will fill in the pixels array
CGContextDrawImage(context,CGRectMake(0,0, width, height), [image CGImage]);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t *rgbaPixel = (uint8_t*) &pixels[y * width + x];
// convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
uint32_t gray = 0.3 * rgbaPixel[RED] +0.59 * rgbaPixel[GREEN] +0.11 * rgbaPixel[BLUE];
// set the pixels to gray
rgbaPixel[RED] = gray;
rgbaPixel[GREEN] = gray;
rgbaPixel[BLUE] = gray;
}
}
// create a new CGImageRef from our context with the modified pixels
CGImageRef imageRef = CGBitmapContextCreateImage(context);
// we're done with the context, color space, and pixels
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);
// make a new UIImage to return
UIImage *resultUIImage = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:UIImageOrientationUp];
// we're done with image now too
CGImageRelease(imageRef);
return resultUIImage;
}
文本變灰
文本textColor變?yōu)榛疑?rgb的處理有很多這里簡(jiǎn)單提供一個(gè)
[self swizzleMethod:self orgSel:@selector(initWithRed:green:blue:alpha:) swizzSel:@selector(hdd_initWithRed:green:blue:alpha:)];
+ (UIColor *)hdd_colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)a{
CGFloat gray = red*0.299 + green*0.587 + blue*0.114;
if ([CSDarkSiteManager isLocalCacheDarkSiteStatus]) {
return [self hdd_colorWithRed:gray green:gray blue:gray alpha:a];
}
else {
return [self hdd_colorWithRed:red green:green blue:blue alpha:a];
}
}
文本變灰色還牽扯到系統(tǒng)顏色的灰色處理,例如[UIcolor whiteColor],系統(tǒng)alertView彈框中的藍(lán)色按鈕顏色處理,都需要有專(zhuān)門(mén)的處理方法
xib圖片變灰
由于xib讀取圖片不是直接通過(guò)[UIimageView imageName:@""] 加載方式 沒(méi)辦法直接通過(guò)hook setImage直接設(shè)置xib圖片變?yōu)榛疑?/p>
方法:重寫(xiě)UIimageView的awakeFromNib,再去執(zhí)行hook setImage的方法進(jìn)行加載
webView的變灰
hook webView的初始化方法,注入js灰色
+ (void)swizzHook {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method originalMethod = class_getInstanceMethod([self class], @selector(initWithFrame:configuration:));
Method swizzledMethod = class_getInstanceMethod([self class], @selector(lg_initWithFrame:configuration:));
method_exchangeImplementations(originalMethod, swizzledMethod);
});
}
- (instancetype)lg_initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration {
Class class = NSClassFromString(@"AIRWKWebView");
if ([CSDarkSiteManager isLocalCacheDarkSiteStatus] && ![self isKindOfClass:class]) {
NSString *jScript = @"var filter = '-webkit-filter:grayscale(100%);-moz-filter:grayscale(100%); -ms-filter:grayscale(100%); -o-filter:grayscale(100%) filter:grayscale(100%);';document.getElementsByTagName('html')[0].style.filter = 'grayscale(100%)';";
// 注入
WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[configuration.userContentController addUserScript:wkUScript];
WKWebView *webView = [self lg_initWithFrame:frame configuration:configuration];
return webView;
}
else {
WKWebView *webView = [self lg_initWithFrame:frame configuration:configuration];
return webView;
}
}
attributeText加載htmlString變灰
attribute情況下加載html不是直接通過(guò)設(shè)置color來(lái)改變顏色的,需要通過(guò)遍歷對(duì)應(yīng)的屬性進(jìn)行重新顏色進(jìn)行賦值
存在的問(wèn)題
這樣全局修改,可能會(huì)把鍵盤(pán)的顏色也改變了需要特殊處理
系統(tǒng)顏色中,alertView中的藍(lán)色,不在定義的系統(tǒng)顏色范圍之內(nèi)需要重新加載
hook系統(tǒng)顏色的處理
完整性
需要完整的私發(fā)信息

到此這篇關(guān)于iOS整個(gè)APP實(shí)現(xiàn)灰色主題的示例代碼的文章就介紹到這了,更多相關(guān)iOS 灰色主題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
iOS AVPlayer切換播放源實(shí)現(xiàn)連續(xù)播放和全屏切換的方法
這篇文章主要給大家介紹了關(guān)于iOS中AVPlayer切換播放源實(shí)現(xiàn)連續(xù)播放和全屏切換的方法,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05
iOS模仿微信長(zhǎng)按識(shí)別二維碼的多種方式
這篇文章主要介紹了iOS模仿微信長(zhǎng)按識(shí)別二維碼的兩種方式,文章第二種方式是識(shí)別網(wǎng)頁(yè)中的二維碼,具體思路詳解大家參考下本文2017-07-07
iOS開(kāi)發(fā)狀態(tài)欄及設(shè)置功能全面詳解
這篇文章主要為大家介紹了iOS開(kāi)發(fā)狀態(tài)欄及設(shè)置功能全面詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法
這篇文章主要介紹了iOS 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組的操作方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07
iOS實(shí)現(xiàn)導(dǎo)航欄透明示例代碼
本篇文章主要介紹了iOS實(shí)現(xiàn)導(dǎo)航欄透明示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
iOS App開(kāi)發(fā)中修改UILabel默認(rèn)字體的方法
UILabel是控制字體顯示的主要方式,這里我們就來(lái)看看通過(guò)NSAttributedText和NSMutableAttributedText這兩個(gè)類(lèi)或者用runtime的方式來(lái)在iOS App開(kāi)發(fā)中修改UILabel默認(rèn)字體的方法2016-07-07
iOS驗(yàn)證手機(jī)號(hào)的正則表達(dá)式
這篇文章主要為大家詳細(xì)介紹了iOS驗(yàn)證手機(jī)號(hào)的正則表達(dá)式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
ios 使用xcode11 新建項(xiàng)目工程的步驟詳解
這篇文章主要介紹了ios 使用xcode11 新建項(xiàng)目工程 (值得注意的問(wèn)題),本文分步驟通過(guò)圖文的形式給大家展示,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
實(shí)例講解iOS音樂(lè)播放器DOUAudioStreamer用法
本篇文章給大家通過(guò)實(shí)例講解了iOS音樂(lè)播放器DOUAudioStreamer用法以及分享了實(shí)例代碼,一起學(xué)習(xí)參考下吧。2017-12-12

