在一些哀悼日,清明节的时候app会实现一些灰色主题功能,部分app需求是tab首页实现灰色模式就可以,但一些需求是直接整个app都变为灰色模。
基本的实现方式1,普通页面用hook颜色方式2.web页面用注入灰色js实现方式
重新绘制图片变为灰色代码实现
//image类别 - (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
文本textColor变为灰色 rgb的处理有很多这里简单提供一个
[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]; } }
文本变灰色还牵扯到系统颜色的灰色处理,例如[UIcolor whiteColor],系统alertView弹框中的蓝色按钮颜色处理,都需要有专门的处理方法
由于xib读取图片不是直接通过[UIimageView imageName:@""] 加载方式 没办法直接通过hook setImage直接设置xib图片变为灰色
方法:重写UIimageView的awakeFromNib,再去执行hook setImage的方法进行加载
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不是直接通过设置color来改变颜色的,需要通过遍历对应的属性进行重新颜色进行赋值
这样全局修改,可能会把键盘的颜色也改变了需要特殊处理
系统颜色中,alertView中的蓝色,不在定义的系统颜色范围之内需要重新加载
hook系统颜色的处理
需要完整的私发信息
到此这篇关于iOS整个APP实现灰色主题的示例代码的文章就介绍到这了,更多相关iOS 灰色主题内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!