ios開發零侵入修改全局主題,無需修改項目舊代碼,可隨時添加、移除,可快速實現“暗黑模式”

ZXTheme

github地址

安裝

通過CocoaPods安裝

pod 'ZXTheme'

手動導入

  • 將ZXTheme拖入項目中。

導入頭文件

#import "ZXTheme.h"

零侵入實現黑暗模式示例

  • 代碼參照Appdelegate.m文件
Image text

說明

ZXTheme用于修改全局UI的主題,設置方法與注意點說明以UILabel為例:

  • 示例代碼
[ZXTheme defaultTheme].zx_labelThemeBlock = ^ZXLabelTheme * _Nonnull(UILabel * _Nonnull label) {
    ZXLabelTheme *labelTheme = [[ZXLabelTheme alloc]init];
    labelTheme.textColor = [UIColor blueColor];
    labelTheme.textAlignment = NSTextAlignmentCenter;
    return labelTheme;
};
  • 說明:我們需要修改全局label的主題,則書寫[ZXTheme defaultTheme].zx_labelThemeBlock
  • 這個block中有一個label對象,代表當前需要設置主題的label
  • 這個block需要一個返回值,在設置label主題的block中,這個返回值是ZXLabelTheme對象,創建這個對象,并更改其中的屬性即可更改對應的label主題
  • 下方示例代碼實現將所有文字內容為“測試”的label文字顏色設置為綠色,其他label的文字顏色不變
[ZXTheme defaultTheme].zx_labelThemeBlock = ^ZXLabelTheme * _Nonnull(UILabel * _Nonnull label) {
    ZXLabelTheme *labelTheme = [[ZXLabelTheme alloc]init];
    if([label.text isEqualToString:@"測試"]){
        labelTheme.textColor = [UIColor greenColor];
    }
    return labelTheme;
};
  • 下方示例代碼實現將所有文字內容為“測試”的label文字顏色設置為綠色,其他label的文字顏色不變,所有label背景色變為紅色
[ZXTheme defaultTheme].zx_labelThemeBlock = ^ZXLabelTheme * _Nonnull(UILabel * _Nonnull label) {
    ZXLabelTheme *labelTheme = [[ZXLabelTheme alloc]init];
    if([label.text isEqualToString:@"測試"]){
        labelTheme.textColor = [UIColor greenColor];
    }
    labelTheme.backgroundColor = [UIColor redColor];
    return labelTheme;
};
  • 下方示例代碼實現將所有文字內容為“測試”的label文字顏色設置為綠色并且背景色變為紅色,其他label主題不變
[ZXTheme defaultTheme].zx_labelThemeBlock = ^ZXLabelTheme * _Nonnull(UILabel * _Nonnull label) {
    ZXLabelTheme *labelTheme = [[ZXLabelTheme alloc]init];
    if([label.text isEqualToString:@"測試"]){
        labelTheme.textColor = [UIColor greenColor];
        labelTheme.backgroundColor = [UIColor redColor];
        return labelTheme;
    }else{
        return nil;
    }
};
  • 由上方三段示例代碼可以得出以下規律:
    1.themeblock返回值為nil,則代表當前label對象不設置主題
    2.ZXLabelTheme對象中屬性沒有設置,則代表當前label對象中對應屬性不設置主題
    3.ZXLabelTheme對象中屬性有值,則當前label對象中對應屬性用ZXLabelTheme對象的對應屬性值,例如labelTheme.textColor有值,則label的textColor為labelTheme.textColor,否則label的textColor為label本身的textColor
  • 注意:ZXTheme中設置的主題擁有最高的管理權限,也就意味著,若您在ZXTheme中設置了對應UI控件的主題,則您在其他地方都無法更改對應UI控件的主題

以上是相關注意點和使用技巧說明,設置其他UI控件主題同理,因此下方不再贅述


所有UI控件主題設置

通用工具函數

  • UIView(Extension)
/**
 獲取view所屬的控制器

 @return view所屬的控制器
 */
- (UIViewController*)zx_getViewController;

/**
 判斷當前view是否是目標view類的子類(包含子類遞歸)

 @param cls 目標view的類
 @return 當前view是否是目標view類的子類(包含子類遞歸)
 */
- (BOOL)zx_inViewClass:(Class)cls;
  • UILabel(Extension)
/**
 是否屬于UIButton
 */
@property(assign, nonatomic,readonly) BOOL zx_belongsToBtn;

/**
 是否屬于UITextField中的placeholder
 */
@property(assign, nonatomic,readonly) BOOL zx_belongsToTextFieldPlaceholder;
  • UIImage(Extension)
/**
 將UIImage渲染為指定顏色

 @param color 渲染顏色
 @return 渲染之后的UIImage
 */
- (UIImage*)renderColor:(UIColor *)color;

ZXTheme

+ (instancetype)defaultTheme;
/**
 是否是暗黑主題
 */
@property(assign, nonatomic, getter=zx_isDarkTheme)BOOL zx_darkTheme;
/**
 發送主題更新通知
 */
- (void)zx_themeUpdate;
/**
 設置UIView的主題
 */
@property(copy, nonatomic)ZXViewTheme *(^zx_viewThemeBlock)(UIView *view);
/**
 設置UILabel的主題
 */
@property(copy, nonatomic)ZXLabelTheme *(^zx_labelThemeBlock)(UILabel *label);
/**
 設置UIButton的主題
 */
@property(copy, nonatomic)ZXButtonTheme *(^zx_buttonThemeBlock)(UIButton *button);
/**
 設置UISegmentedControl的主題
 */
@property(copy, nonatomic)ZXSegmentedControlTheme *(^zx_segmentedControlThemeBlock)(UISegmentedControl *segmentedControl);
/**
 設置UITextField的主題
 */
@property(copy, nonatomic)ZXTextFieldTheme *(^zx_textFieldThemeBlock)(UITextField *textField);
/**
 設置UISlider的主題
 */
@property(copy, nonatomic)ZXSliderTheme *(^zx_sliderThemeBlock)(UISlider *slider);
/**
 設置UISwitch的主題
 */
@property(copy, nonatomic)ZXSwitchTheme *(^zx_switchThemeBlock)(UISwitch *mySwitch);
/**
 設置UIActivityIndicatorView的主題
 */
@property(copy, nonatomic)ZXActivityIndicatorViewTheme *(^zx_activityIndicatorViewThemeBlock)(UIActivityIndicatorView *activityIndicatorView);
/**
 設置UIProgressView的主題
 */
@property(copy, nonatomic)ZXProgressViewTheme *(^zx_progressViewThemeBlock)(UIProgressView *progressView);
/**
 設置UIPageControl的主題
 */
@property(copy, nonatomic)ZXPageControlTheme *(^zx_pageControlThemeBlock)(UIPageControl *pageControl);
/**
 設置UIStepper的主題
 */
@property(copy, nonatomic)ZXStepperTheme *(^zx_stepperThemeBlock)(UIStepper *stepper);
/**
 設置UIImageView的主題
 */
@property(copy, nonatomic)ZXImageViewTheme *(^zx_imageViewThemeBlock)(UIImageView *imageView);
/**
 設置UITextView的主題
 */
@property(copy, nonatomic)ZXTextViewTheme *(^zx_textViewThemeBlock)(UITextView *textView);
/**
 設置UITabBar的主題
 */
@property(copy, nonatomic)ZXTabBarTheme *(^zx_tabBarThemeBlock)(UITabBar *tabBar);
/**
 設置UITabBarItem的主題
 */
@property(copy, nonatomic)ZXTabBarItemTheme *(^zx_tabBarItemThemeBlock)(UITabBarItem *tabBarItem);
/**
 設置UINavigationBar的主題
 */
@property(copy, nonatomic)ZXNavigationBarTheme *(^zx_navigationBarThemeBlock)(UINavigationBar *navigationBar);
/**
 設置UIBarButtonItem的主題
 */
@property(copy, nonatomic)ZXBarButtonItemTheme *(^zx_barButtonItemThemeBlock)(UIBarButtonItem *barButtonItem);
/**
 設置UITableView的主題
 */
@property(copy, nonatomic)ZXTableViewTheme *(^zx_tableViewThemeBlock)(UITableView *tableView);
/**
 設置UICollectionView的主題
 */
@property(copy, nonatomic)ZXCollectionViewTheme *(^zx_collectionViewThemeBlock)(UICollectionView *collectionView);

UIView

  • 設置view主題
[ZXTheme defaultTheme].zx_viewThemeBlock = ^ZXViewTheme * _Nonnull(UIView * _Nonnull view) {
    ZXViewTheme *viewTheme = [[ZXViewTheme alloc]init];
    if([view.nextResponder isKindOfClass:[UIViewController class]]){
        viewTheme.backgroundColor = [self getControllerBacViewColor];
    }
    return viewTheme;
};
  • ZXStepperTheme所有屬性
/**
 設置背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UILabel

  • 設置label主題
[ZXTheme defaultTheme].zx_labelThemeBlock = ^ZXLabelTheme * _Nonnull(UILabel * _Nonnull label) {
    ZXLabelTheme *labelTheme = [[ZXLabelTheme alloc]init];
    labelTheme.textColor = [UIColor redColor];
    labelTheme.font = [UIFont systemFontOfSize:14];
    labelTheme.textAlignment = NSTextAlignmentCenter;
    labelTheme.lineBreakMode = NSLineBreakByCharWrapping;
    labelTheme.backgroundColor = [UIColor redColor];
    return labelTheme;
};
  • ZXLabelTheme所有屬性
/**
 設置文字顏色
 */
@property(strong, nonatomic) UIColor *textColor;
/**
 設置文字字體
 */
@property(strong, nonatomic) UIFont *font;
/**
 設置文字對齊方式
 */
@property(assign, nonatomic) NSTextAlignment textAlignment;
/**
 設置文字換行模式
 */
@property(assign, nonatomic) NSLineBreakMode lineBreakMode;
/**
 設置文字是否高亮
 */
@property(assign, nonatomic) BOOL highlighted;
/**
 設置文字高亮時候的顏色
 */
@property(strong, nonatomic) UIColor *highlightedTextColor;
/**
 設置文字是否根據寬度自動調整字體
 */
@property(assign, nonatomic) BOOL adjustsFontSizeToFitWidth;
/**
 設置文字最大行數
 */
@property(assign, nonatomic) long long numberOfLines;
/**
 設置文字背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UIButton

  • 設置button主題
[ZXTheme defaultTheme].zx_buttonThemeBlock = ^ZXButtonTheme * _Nonnull(UIButton * _Nonnull button) {
    ZXButtonTheme *buttonTheme = [[ZXButtonTheme alloc]init];
    [buttonTheme setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [buttonTheme setBackgroundImage:[UIImage imageNamed:@"kanjia"] forState:UIControlStateNormal];
    buttonTheme.backgroundColor = [UIColor yellowColor];
    return buttonTheme;
};
  • ZXButtonTheme所有屬性
/**
 設置按鈕tintColor
 */
@property(strong, nonatomic) UIColor *tintColor;
/**
 根據狀態設置按鈕文字顏色
 
 @param color 按鈕文字顏色
 @param state 按鈕狀態
 */
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
/**
 按鈕文字顏色與狀態映射的字典
 */
@property(strong, nonatomic,readonly) NSMutableDictionary *titleColorMapper;

/**
 根據狀態設置按鈕文字陰影顏色
 
 @param color 文字陰影顏色
 @param state 按鈕狀態
 */
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
/**
 按鈕文字陰影顏色與狀態映射的字典
 */
@property(strong, nonatomic,readonly) NSMutableDictionary *titleShadowColorMapper;
/**
 根據狀態設置按鈕圖片
 
 @param image 按鈕圖片
 @param state 按鈕狀態
 */
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
/**
 按鈕圖片與狀態映射的字典
 */
@property(strong, nonatomic,readonly) NSMutableDictionary *imageMapper;
/**
 根據狀態設置按鈕背景圖片
 
 @param image 按鈕背景圖片
 @param state 按鈕狀態
 */
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
/**
 按鈕背景圖片與狀態映射的字典
 */
@property(strong, nonatomic,readonly) NSMutableDictionary *backgroundImageMapper;
/**
 根據狀態設置按鈕圖片顏色
 
 @param color 按鈕圖片顏色
 @param state 按鈕狀態
 */
- (void)setImageColor:(UIColor *)color forState:(UIControlState)state;
/**
 按鈕圖片顏色與狀態映射的字典
 */
@property(strong, nonatomic,readonly) NSMutableDictionary *imageColorMapper;

/**
 根據狀態設置按鈕背景圖片顏色
 
 @param color 按鈕背景圖片顏色
 @param state 按鈕狀態
 */
- (void)setBackgroundImageColor:(UIColor *)color forState:(UIControlState)state;
/**
 按鈕背景圖片顏色與狀態映射的字典
 */
@property(strong, nonatomic,readonly) NSMutableDictionary *backgroundImageColorMapper;

/**
 設置按鈕背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UISegmentedControl

  • 設置segmentedControl主題
[ZXTheme defaultTheme].zx_segmentedControlThemeBlock = ^ZXSegmentedControlTheme * _Nonnull(UISegmentedControl * _Nonnull segmentedControl) {
    ZXSegmentedControlTheme *segmentedControlTheme = [[ZXSegmentedControlTheme alloc]init];
    segmentedControlTheme.tintColor = [UIColor redColor];
    return segmentedControlTheme;
};
  • ZXSegmentedControlTheme所有屬性
/**
 設置SegmentedControl的tintColor
 */
@property(strong, nonatomic) UIColor *tintColor;
/**
 設置SegmentedControl的背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UITextField

  • 設置textField主題
[ZXTheme defaultTheme].zx_textFieldThemeBlock = ^ZXTextFieldTheme * _Nonnull(UITextField * _Nonnull textField) {
    ZXTextFieldTheme *textFieldTheme = [[ZXTextFieldTheme alloc]init];
    textFieldTheme.tintColor = [UIColor redColor];
    textFieldTheme.textColor = [UIColor blueColor];
    textFieldTheme.placeholderColor = [UIColor redColor];
    return textFieldTheme;
};
  • ZXTextFieldTheme所有屬性
/**
 設置TextField顏色
 */
@property(strong, nonatomic) UIColor *textColor;
/**
 設置TextField字體
 */
@property(strong, nonatomic) UIFont *font;
/**
 設置TextField對齊方式
 */
@property(assign, nonatomic) NSTextAlignment textAlignment;
/**
 設置TextField的tintColor
 */
@property(strong, nonatomic) UIColor *tintColor;
/**
 設置TextField的placeholder顏色
 */
@property(strong, nonatomic) UIColor *placeholderColor;
/**
 設置TextField的背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UISlider

  • 設置slider主題
[ZXTheme defaultTheme].zx_sliderThemeBlock = ^ZXSliderTheme * _Nonnull(UISlider * _Nonnull slider) {
    ZXSliderTheme *sliderTheme = [[ZXSliderTheme alloc]init];
    sliderTheme.minimumTrackTintColor = [UIColor redColor];
    sliderTheme.maximumTrackTintColor = [UIColor blueColor];
    sliderTheme.thumbTintColor = [UIColor yellowColor];
    return sliderTheme;
};
  • ZXSliderTheme所有屬性
/**
 設置Slider左側背景顏色
 */
@property(strong, nonatomic) UIColor *minimumTrackTintColor;
/**
 設置Slider右側背景顏色
 */
@property(strong, nonatomic) UIColor *maximumTrackTintColor;
/**
 設置Slider滑塊顏色
 */
@property(strong, nonatomic) UIColor *thumbTintColor;
/**
 設置Slider左側圖片
 */
@property(strong, nonatomic) UIImage *minimumValueImage;
/**
 設置Slider右側圖片
 */
@property(strong, nonatomic) UIImage *maximumValueImage;
/**
 設置Slider背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UISwitch

  • 設置switch主題
[ZXTheme defaultTheme].zx_switchThemeBlock = ^ZXSwitchTheme * _Nonnull(UISwitch * _Nonnull mySwitch) {
    ZXSwitchTheme *switchTheme = [[ZXSwitchTheme alloc]init];
    switchTheme.onTintColor = [UIColor redColor];
    switchTheme.tintColor = [UIColor redColor];
    return switchTheme;
};
  • ZXSwitchTheme所有屬性
/**
 設置Switch的tintColor
 */
@property(strong, nonatomic) UIColor *tintColor;
/**
 設置Switch的onTintColor
 */
@property(strong, nonatomic) UIColor *onTintColor;
/**
 設置Switch背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UIActivityIndicatorView

  • 設置activityIndicatorView主題
[ZXTheme defaultTheme].zx_activityIndicatorViewThemeBlock = ^ZXActivityIndicatorViewTheme * _Nonnull(UIActivityIndicatorView * _Nonnull activityIndicatorView) {
    ZXActivityIndicatorViewTheme *activityIndicatorViewTheme = [[ZXActivityIndicatorViewTheme alloc]init];
    activityIndicatorViewTheme.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    return activityIndicatorViewTheme;
};
  • ZXActivityIndicatorViewTheme所有屬性
/**
 設置ActivityIndicatorView樣式
 */
@property(assign, nonatomic) UIActivityIndicatorViewStyle activityIndicatorViewStyle;
/**
 設置ActivityIndicatorView背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UIProgressView

  • 設置progressView主題
[ZXTheme defaultTheme].zx_progressViewThemeBlock = ^ZXProgressViewTheme * _Nonnull(UIProgressView * _Nonnull progressView) {
    ZXProgressViewTheme *progressViewTheme = [[ZXProgressViewTheme alloc]init];
    progressViewTheme.progressTintColor = [UIColor redColor];
    progressViewTheme.trackTintColor = [UIColor blueColor];
    return progressViewTheme;
};
  • ZXProgressViewTheme所有屬性
/**
 設置ProgressView已加載的進度條顏色
 */
@property(strong, nonatomic) UIColor *progressTintColor;
/**
 設置ProgressView未加載的進度條顏色
 */
@property(strong, nonatomic) UIColor *trackTintColor;
/**
 設置ProgressView已加載的進度條圖片
 */
@property(strong, nonatomic) UIImage *progressImage;
/**
 設置ProgressView未加載的進度條圖片
 */
@property(strong, nonatomic) UIColor *trackImage;

UIPageControl

  • 設置pageControl主題
[ZXTheme defaultTheme].zx_pageControlThemeBlock = ^ZXPageControlTheme * _Nonnull(UIPageControl * _Nonnull pageControl) {
    ZXPageControlTheme *pageControlTheme = [[ZXPageControlTheme alloc]init];
    pageControlTheme.pageIndicatorTintColor = [UIColor redColor];
    pageControlTheme.currentPageIndicatorTintColor = [UIColor blueColor];
    return pageControlTheme;
};
  • ZXPageControlTheme所有屬性
/**
 設置PageControl其他頁的顏色
 */
@property(strong, nonatomic) UIColor *pageIndicatorTintColor;
/**
 設置PageControl當前頁的顏色
 */
@property(strong, nonatomic) UIColor *currentPageIndicatorTintColor;

UIStepper

  • 設置stepper主題
[ZXTheme defaultTheme].zx_stepperThemeBlock = ^ZXStepperTheme * _Nonnull(UIStepper * _Nonnull stepper) {
    ZXStepperTheme *stepperTheme = [[ZXStepperTheme alloc]init];
    stepperTheme.tintColor = [UIColor whiteColor];
    return stepperTheme;
};
  • ZXStepperTheme所有屬性
/**
 設置Stepper的tintColor
 */
@property(strong, nonatomic) UIColor *tintColor;
/**
 設置Stepper背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UIImageView

  • 設置imageView主題
[ZXTheme defaultTheme].zx_imageViewThemeBlock = ^ZXImageViewTheme * _Nonnull(UIImageView * _Nonnull imageView) {
    if(imageView.frame.size.width == 50){
        ZXImageViewTheme *imageViewTheme = [[ZXImageViewTheme alloc]init];
        imageViewTheme.imageColor = [UIColor redColor];
        imageViewTheme.backgroundColor = [UIColor yellowColor];
        return imageViewTheme;
    }else{
        return nil;
    }
};
  • ZXImageViewTheme所有屬性
/**
 設置ImageView的image
 */
@property(strong, nonatomic) UIImage *image;
/**
 設置ImageView的image顏色
 */
@property(strong, nonatomic) UIColor *imageColor;
/**
 設置ImageView的highlightedImage
 */
@property(strong, nonatomic) UIImage *highlightedImage;
/**
 設置ImageView的highlightedImage顏色
 */
@property(strong, nonatomic) UIColor *highlightedImageColor;
/**
 設置ImageView的動畫image數組
 */
@property(strong, nonatomic) NSArray *animationImages;
/**
 設置ImageView的image數組的顏色
 */
@property(strong, nonatomic) UIColor *animationImagesColor;
/**
 設置ImageView的動畫highlightedImage數組
 */
@property(strong, nonatomic) NSArray *highlightedAnimationImages;
/**
 設置ImageView的highlightedAnimationImages數組的顏色
 */
@property(strong, nonatomic) UIColor *highlightedAnimationImagesColor;
/**
 設置ImageView的tintColor
 */
@property(strong, nonatomic) UIColor *tintColor;
/**
 設置ImageView背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UITextView

  • 設置textView主題
[ZXTheme defaultTheme].zx_textViewThemeBlock = ^ZXTextViewTheme * _Nonnull(UITextView * _Nonnull textView) {
    ZXTextViewTheme *textViewTheme = [[ZXTextViewTheme alloc]init];
    textViewTheme.textColor = [UIColor redColor];
    textViewTheme.backgroundColor = [UIColor blueColor];
    return textViewTheme;
};
  • ZXTextViewTheme所有屬性
/**
 設置文字顏色
 */
@property(strong, nonatomic) UIColor *textColor;
/**
 設置文字字體
 */
@property(strong, nonatomic) UIFont *font;
/**
 設置文字對齊方式
 */
@property(assign, nonatomic) NSTextAlignment textAlignment;
/**
 設置背景顏色
 */
@property(strong, nonatomic) UIColor *backgroundColor;

UITabBar

  • 設置tabBar主題
[ZXTheme defaultTheme].zx_tabBarThemeBlock  = ^ZXTabBarTheme * _Nonnull(UITabBar * _Nonnull tabBar) {
    ZXTabBarTheme *tabBarTheme = [[ZXTabBarTheme alloc]init];
    tabBarTheme.translucent = NO;
    tabBarTheme.barTintColor = [self getTabbarTintColor];
    return tabBarTheme;
};
  • ZXTabBarTheme所有屬性
/**
 設置Tabbar的tintColor
 */
@property(strong, nonatomic) UIColor *tintColor;
/**
 設置Tabbar是否透明
 */
@property(assign, nonatomic) BOOL translucent;
/**
 設置Tabbar的背景色
 */
@property(strong, nonatomic) UIColor *barTintColor;
/**
 設置Tabbar的背景圖片
 */
@property(strong, nonatomic) UIImage *backgroundImage;
/**
 設置Tabbar的selectionIndicatorImage
 */
@property(strong, nonatomic) UIImage *selectionIndicatorImage;
/**
 設置Tabbar的shadowImage
 */
@property(strong, nonatomic) UIImage *shadowImage;

UITabBarItem

  • 設置tabBarItem主題
[ZXTheme defaultTheme].zx_tabBarItemThemeBlock = ^ZXTabBarItemTheme * _Nonnull(UITabBarItem * _Nonnull tabBarItem) {
    ZXTabBarItemTheme *tabBarItemTheme = [[ZXTabBarItemTheme alloc]init];
    tabBarItemTheme.selectedImageColor = [UIColor redColor];
    [tabBarItemTheme setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11],NSFontAttributeName, [UIColor redColor],NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    return tabBarItemTheme;
};
  • ZXTabBarItemTheme所有屬性
/**
 設置TabBarItem的image
 */
@property(strong, nonatomic) UIImage *image;
/**
 設置TabBarItem的image顏色
 */
@property(strong, nonatomic) UIColor *imageColor;
/**
 設置TabBarItem的selectedImage
 */
@property(strong, nonatomic) UIImage *selectedImage;
/**
 設置TabBarItem的selectedImage顏色
 */
@property(strong, nonatomic) UIColor *selectedImageColor;

/**
 根據狀態設置TabBarItem文字屬性
 
 @param attributes 文字陰影顏色
 @param state 按鈕狀態
 */
- (void)setTitleTextAttributes:(nullable NSDictionary<NSAttributedStringKey,id> *)attributes forState:(UIControlState)state;
/**
 TabBarItem文字屬性與狀態映射的字典
 */
@property(strong, nonatomic,readonly) NSMutableDictionary *titleTextAttributesMapper;

UINavigationBar

  • 設置navigationBar主題
[ZXTheme defaultTheme].zx_navigationBarThemeBlock = ^ZXNavigationBarTheme * _Nonnull(UINavigationBar * _Nonnull navigationBar) {
    ZXNavigationBarTheme *navigationBarTheme = [[ZXNavigationBarTheme alloc]init];
    navigationBarTheme.translucent = NO;
    navigationBarTheme.barTintColor = UIColorFromRGB(0x1c1c1c);
    navigationBarTheme.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:20]};
    return navigationBarTheme;
};
  • ZXNavigationBarTheme所有屬性
/**
 設置NavigationBar的tintColor
 */
@property(strong, nonatomic) UIColor *tintColor;
/**
 設置NavigationBar是否透明
 */
@property(assign, nonatomic) BOOL translucent;
/**
 設置NavigationBar的背景色
 */
@property(strong, nonatomic) UIColor *barTintColor;
/**
 設置NavigationBar的shadowImage
 */
@property(strong, nonatomic) UIImage *shadowImage;
/**
 設置NavigationBar的titleTextAttributes
 */
@property(strong, nonatomic) NSDictionary<NSAttributedStringKey, id> *titleTextAttributes;

UIBarButtonItem

  • 設置barButtonItem主題
[ZXTheme defaultTheme].zx_barButtonItemThemeBlock = ^ZXBarButtonItemTheme * _Nonnull(UIBarButtonItem * _Nonnull barButtonItem){
    ZXBarButtonItemTheme *barButtonItemTheme = [[ZXBarButtonItemTheme alloc]init];
    barButtonItemTheme.tintColor = [UIColor redColor];
    return barButtonItemTheme;
};
  • ZXBarButtonItemTheme所有屬性
/**
 設置BarButtonItem的style
 */
@property(assign, nonatomic) UIBarButtonItemStyle style;
/**
 設置BarButtonItem的tintColor
 */
@property(strong, nonatomic) UIColor *tintColor;
/**
 設置BarButtonItem的customView
 */
@property(strong, nonatomic) UIView *customView;

UITableView

  • 設置tableView主題
[ZXTheme defaultTheme].zx_tableViewThemeBlock = ^ZXTableViewTheme * _Nonnull(UITableView * _Nonnull tableView) {
    ZXTableViewTheme *tableViewTheme = [[ZXTableViewTheme alloc]init];
    tableViewTheme.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableViewTheme.backgroundColor = [self getTableViewBacColor];
    tableViewTheme.viewForHeaderInSection = ^UIView * _Nonnull(UIView * _Nonnull headerView, NSUInteger section) {
        headerView.backgroundColor = [self getTableViewHeaderViewBacColor];
        for (UIView *view in headerView.subviews) {
            if([view isKindOfClass:[UILabel class]]){
                ((UILabel *)view).textColor = [self getTableViewHeaderViewLabelTextColor];
            }
            
        }
        return headerView;
    };
    tableViewTheme.cellForRowAtIndexPath = ^UITableViewCell * _Nonnull(UITableViewCell * _Nonnull cell, NSIndexPath * _Nonnull indexPath) {
        cell.backgroundColor = [self getTableViewCellBacColor];
        for (UIView *view in cell.contentView.subviews) {
            if([view isKindOfClass:[UILabel class]]){
                ((UILabel *)view).textColor = [self getTableViewCellLabelTextColor];
            }
            if([view isKindOfClass:[UIImageView class]]){
                ((UIImageView *)view).image = [((UIImageView *)view).image renderColor:[self getTableViewCellImageViewRenderColor]];
            }
        }
        return cell;
    };
    return tableViewTheme;
};
  • ZXTableViewTheme所有屬性
/**
 設置TableView的backgroundColor
 */
@property(strong, nonatomic) UIColor *backgroundColor;
/**
 設置TableView的backgroundView
 */
@property(strong, nonatomic) UIView *backgroundView;
/**
 設置TableView的sectionIndexColor
 */
@property(strong, nonatomic) UIColor *sectionIndexColor;
/**
 設置TableView的sectionIndexBackgroundColor
 */
@property(strong, nonatomic) UIColor *sectionIndexBackgroundColor;
/**
 設置TableView的sectionIndexTrackingBackgroundColor
 */
@property(strong, nonatomic) UIColor *sectionIndexTrackingBackgroundColor;
/**
 設置TableView的separatorStyle
 */
@property(assign, nonatomic) UITableViewCellSeparatorStyle separatorStyle;
/**
 設置TableView的separatorColor
 */
@property(strong, nonatomic) UIColor *separatorColor;
/**
 設置TableView的separatorEffect
 */
@property(strong, nonatomic) UIVisualEffect *separatorEffect;
/**
 設置TableView的tableHeaderView
 */
@property(strong, nonatomic) UIView *tableHeaderView;
/**
 設置TableView的tableFooterView
 */
@property(strong, nonatomic) UIView *tableFooterView;
/**
 設置TableView的cell
 */
@property(copy, nonatomic) UITableViewCell *(^cellForRowAtIndexPath)(UITableViewCell *cell,NSIndexPath *indexPath);
/**
 設置TableView的headerView
 */
@property(copy, nonatomic) UIView *(^viewForHeaderInSection)(UIView *headerView,NSUInteger section);
/**
 設置TableView的footerView
 */
@property(copy, nonatomic) UIView *(^viewForFooterInSection)(UIView *footerView,NSUInteger section);

UICollectionView

  • 設置collectionView主題
[ZXTheme defaultTheme].zx_collectionViewThemeBlock = ^ZXCollectionViewTheme * _Nonnull(UICollectionView * _Nonnull collectionView) {
    ZXCollectionViewTheme *collectionViewTheme = [[ZXCollectionViewTheme alloc]init];
    collectionViewTheme.backgroundColor = [self getCollectionViewBacColor];
    collectionViewTheme.cellForItemAtIndexPath = ^UICollectionViewCell * _Nonnull(UICollectionViewCell * _Nonnull cell, NSIndexPath * _Nonnull indexPath) {
        cell.backgroundColor = [self getCollectionViewCellBacColor];
        for (UIView *view in cell.contentView.subviews) {
            if([view isKindOfClass:[UILabel class]]){
                ((UILabel *)view).textColor = [self getCollectionViewCellLabelTextColor];
            }
        }
        return cell;
    };
    collectionViewTheme.viewForSupplementaryElement = ^UICollectionReusableView * _Nonnull(UICollectionReusableView * _Nonnull reusableView, NSString * _Nonnull kind, NSIndexPath * _Nonnull indexPath) {
        reusableView.backgroundColor = [self getCollectionViewHeaderViewBacColor];
        for (UIView *view in reusableView.subviews) {
            if([view isKindOfClass:[UILabel class]]){
                ((UILabel *)view).textColor = [self getCollectionViewHeaderViewLabelTextColor];
            }
            
        }
        return reusableView;
    };
    return collectionViewTheme;
};
  • ZXCollectionViewTheme所有屬性
/**
 設置CollectionView的backgroundColor
 */
@property(strong, nonatomic) UIColor *backgroundColor;
/**
 設置CollectionView的backgroundView
 */
@property(strong, nonatomic) UIView *backgroundView;
/**
 設置CollectionView的cell
 */
@property(copy, nonatomic) UICollectionViewCell *(^cellForItemAtIndexPath)(UICollectionViewCell *cell,NSIndexPath *indexPath);
/**
 設置CollectionView的headerView和FfooterView
 */
@property(copy, nonatomic) UICollectionReusableView *(^viewForSupplementaryElement)(UICollectionReusableView *reusableView,NSString *kind,NSIndexPath *indexPath);

自定義其他View或屬性的主題

  • 例如需要添加UIAlertController的主題設置
  • 創建UIAlertController的分類
  • 在.m文件中實現
#import "UIAlertController+ZXCustomTheme.h"
#import "ZXTheme.h"
@implementation UIAlertController (ZXCustomTheme)
+ (void)load{
    [self zx_handleThemeWithTargetSelector:@selector(addAction:) handleSelector:@selector(zx_addAction:)];
}
- (void)zx_addAction:(UIAlertAction *)action{
    [action setValue:[UIColor redColor] forKey:@"titleTextColor"];
    [self zx_addAction:action];
}
@end
  • 以上代碼實現將UIAlertController的actions文字顏色變為紅色的效果
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,546評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,570評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 176,505評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,017評論 1 313
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,786評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,219評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,287評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,438評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,971評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,796評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,995評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,540評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,230評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,918評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,697評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內容

  • ¥開啟¥ 【iAPP實現進入界面執行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 6,483評論 0 17
  • 親子時間管理踐行D160 打卡日期:20180916 周日 第2個90天打卡累計天數:70/90 #宣言:讓優秀成...
    漫定投閱讀 146評論 0 0
  • 各位小姐姐 美少女們在和男票約會時有沒有遭遇過男友媽媽電話微信瘋狂轟炸? 男友刷100塊的卡都準會接到媽媽打來的“...
    不能再賤的斜杠少女閱讀 323評論 0 0
  • 這幾天,朋友圈被這張圖片刷屏了 隨后,又被這張圖片打臉了。 許多人盯著一個億,或眼紅忌妒,或生無可戀,或雞血滿貫。...
    奇言巷語閱讀 283評論 0 1
  • 所有真諦都在這黃昏里 太陽的余暉 你的影子里 而最好的你在相識后 所有想你的時光里 光是你 影也是你 山的盼望...
    吾乃某山某某某閱讀 331評論 0 0