热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

iOS整个APP实现灰色主题的示例代码

这篇文章主要介绍了iOS整个APP实现灰色主题的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

灰色主题

背景

在一些哀悼日,清明节的时候app会实现一些灰色主题功能,部分app需求是tab首页实现灰色模式就可以,但一些需求是直接整个app都变为灰色模。

  •  普通UI界面
  • web页面
  • xib界面
  • attributeText加载的htmlString页面
  • attachment挂件页面

实现方式

基本的实现方式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图片变灰

由于xib读取图片不是直接通过[UIimageView imageName:@""] 加载方式 没办法直接通过hook setImage直接设置xib图片变为灰色

方法:重写UIimageView的awakeFromNib,再去执行hook setImage的方法进行加载

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不是直接通过设置color来改变颜色的,需要通过遍历对应的属性进行重新颜色进行赋值

存在的问题

这样全局修改,可能会把键盘的颜色也改变了需要特殊处理

系统颜色中,alertView中的蓝色,不在定义的系统颜色范围之内需要重新加载

hook系统颜色的处理

完整性

需要完整的私发信息

到此这篇关于iOS整个APP实现灰色主题的示例代码的文章就介绍到这了,更多相关iOS 灰色主题内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!


推荐阅读
author-avatar
广东蒗缦m莎
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有