如何在iOS中实现一个灰色主题?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
基本的实现方式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 < 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变为灰色 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中实现一个灰色主题问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。