首页 > 编程语言 > iOS整个APP实现灰色主题的示例代码
2021
02-20

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 < 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图片变灰

由于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 灰色主题内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧