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文字顏色變為紅色的效果
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

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