11//
2- // QMUIConfigurationManager .m
2+ // QMUIConfiguration .m
33// qmui
44//
55// Created by QQMail on 15/3/29.
66// Copyright (c) 2015年 QMUI Team. All rights reserved.
77//
88
9- #import " QMUIConfigurationManager.h"
10- #import " QMUICommonDefines.h"
119#import " QMUIConfiguration.h"
10+ #import " QMUICommonDefines.h"
11+ #import " QMUIConfigurationMacros.h"
1212#import " UIImage+QMUI.h"
1313#import " QMUIButton.h"
1414#import " QMUITabBarViewController.h"
1515
16- @implementation QMUIConfigurationManager
16+ @implementation QMUIConfiguration
1717
18- + (QMUIConfigurationManager *) sharedInstance {
18+ + (instancetype ) sharedInstance {
1919 static dispatch_once_t pred;
20- static QMUIConfigurationManager *sharedInstance = nil ;
20+ static QMUIConfiguration *sharedInstance = nil ;
2121 dispatch_once (&pred, ^{
22- sharedInstance = [[QMUIConfigurationManager alloc ] init ];
22+ sharedInstance = [[QMUIConfiguration alloc ] init ];
2323 });
2424 return sharedInstance;
2525}
2626
2727- (instancetype )init {
2828 self = [super init ];
2929 if (self) {
30+ [self initDefaultConfiguration ];
3031 }
3132 return self;
3233}
@@ -86,7 +87,7 @@ - (void)initDefaultConfiguration {
8687
8788 #pragma mark - UITextField & UITextView
8889
89- self.textFieldTintColor = UIColorBlue ;
90+ self.textFieldTintColor = self. blueColor ;
9091 self.textFieldTextInsets = UIEdgeInsetsMake (0 , 7 , 0 , 7 );
9192
9293 #pragma mark - NavigationBar
@@ -162,23 +163,23 @@ - (void)initDefaultConfiguration {
162163 self.tableViewCellSelectedBackgroundColor = UIColorMake (232 , 232 , 232 );
163164 self.tableViewCellWarningBackgroundColor = self.yellowColor ;
164165 self.tableViewCellDisclosureIndicatorImage = [UIImage qmui_imageWithShape: QMUIImageShapeDisclosureIndicator size: CGSizeMake (8 , 13 ) tintColor: UIColorMakeWithRGBA (0 , 0 , 0 , .2 )];
165- self.tableViewCellCheckmarkImage = [UIImage qmui_imageWithShape: QMUIImageShapeCheckmark size: CGSizeMake (15 , 12 ) tintColor: UIColorBlue ];
166+ self.tableViewCellCheckmarkImage = [UIImage qmui_imageWithShape: QMUIImageShapeCheckmark size: CGSizeMake (15 , 12 ) tintColor: self .blueColor ];
166167
167168 self.tableViewSectionHeaderBackgroundColor = UIColorMake (244 , 244 , 244 );
168169 self.tableViewSectionFooterBackgroundColor = UIColorMake (244 , 244 , 244 );
169170 self.tableViewSectionHeaderFont = UIFontBoldMake (12 );
170171 self.tableViewSectionFooterFont = UIFontBoldMake (12 );
171- self.tableViewSectionHeaderTextColor = UIColorGrayDarken ;
172- self.tableViewSectionFooterTextColor = UIColorGray ;
172+ self.tableViewSectionHeaderTextColor = self. grayDarkenColor ;
173+ self.tableViewSectionFooterTextColor = self. grayColor ;
173174 self.tableViewSectionHeaderHeight = 20 ;
174175 self.tableViewSectionFooterHeight = 0 ;
175176 self.tableViewSectionHeaderContentInset = UIEdgeInsetsMake (4 , 15 , 4 , 15 );
176177 self.tableViewSectionFooterContentInset = UIEdgeInsetsMake (4 , 15 , 4 , 15 );
177178
178179 self.tableViewGroupedSectionHeaderFont = UIFontMake (12 );
179180 self.tableViewGroupedSectionFooterFont = UIFontMake (12 );
180- self.tableViewGroupedSectionHeaderTextColor = UIColorGrayDarken ;
181- self.tableViewGroupedSectionFooterTextColor = UIColorGray ;
181+ self.tableViewGroupedSectionHeaderTextColor = self. grayDarkenColor ;
182+ self.tableViewGroupedSectionFooterTextColor = self. grayColor ;
182183 self.tableViewGroupedSectionHeaderHeight = 15 ;
183184 self.tableViewGroupedSectionFooterHeight = 1 ;
184185 self.tableViewGroupedSectionHeaderContentInset = UIEdgeInsetsMake (16 , 15 , 8 , 15 );
@@ -196,65 +197,133 @@ - (void)initDefaultConfiguration {
196197 self.hidesBottomBarWhenPushedInitially = YES ;
197198}
198199
199- @end
200+ - (void )setNavBarBarTintColor : (UIColor *)navBarBarTintColor {
201+ _navBarBarTintColor = navBarBarTintColor;
202+ [UINavigationBar appearance ].barTintColor = _navBarBarTintColor;
203+ }
200204
201- @implementation QMUIConfigurationManager (UIAppearance)
205+ - (void )setNavBarShadowImage : (UIImage *)navBarShadowImage {
206+ _navBarShadowImage = navBarShadowImage;
207+ [UINavigationBar appearance ].shadowImage = _navBarShadowImage;
208+ }
202209
203- + (void )renderGlobalAppearances {
204-
205- // QMUIButton
206- [QMUINavigationButton renderNavigationButtonAppearanceStyle ];
207- [QMUIToolbarButton renderToolbarButtonAppearanceStyle ];
208-
209- // UINavigationBar
210- UINavigationBar *navigationBarAppearance = [UINavigationBar appearance ];
211- navigationBarAppearance.barTintColor = NavBarBarTintColor;
212- navigationBarAppearance.shadowImage = NavBarShadowImage;
213- [navigationBarAppearance setBackgroundImage: NavBarBackgroundImage forBarMetrics: UIBarMetricsDefault];
214-
215- UIFont *navigationBarTitleFont = NavBarTitleFont;
216- UIColor *navigationBarTitleColor = NavBarTitleColor;
217- if (navigationBarTitleFont || navigationBarTitleColor) {
210+ - (void )setNavBarBackgroundImage : (UIImage *)navBarBackgroundImage {
211+ _navBarBackgroundImage = navBarBackgroundImage;
212+ [[UINavigationBar appearance ] setBackgroundImage: _navBarBackgroundImage forBarMetrics: UIBarMetricsDefault];
213+ }
214+
215+ - (void )setNavBarTitleFont : (UIFont *)navBarTitleFont {
216+ _navBarTitleFont = navBarTitleFont;
217+ if (self.navBarTitleFont || self.navBarTitleColor ) {
218218 NSMutableDictionary <NSString *, id > *titleTextAttributes = [[NSMutableDictionary alloc ] init ];
219- if (navigationBarTitleFont ) {
220- [titleTextAttributes setValue: navigationBarTitleFont forKey: NSFontAttributeName ];
219+ if (self. navBarTitleFont ) {
220+ [titleTextAttributes setValue: self .navBarTitleFont forKey: NSFontAttributeName ];
221221 }
222- if (navigationBarTitleColor ) {
223- [titleTextAttributes setValue: navigationBarTitleColor forKey: NSForegroundColorAttributeName ];
222+ if (self. navBarTitleColor ) {
223+ [titleTextAttributes setValue: self .navBarTitleColor forKey: NSForegroundColorAttributeName ];
224224 }
225- navigationBarAppearance .titleTextAttributes = titleTextAttributes;
225+ [UINavigationBar appearance ] .titleTextAttributes = titleTextAttributes;
226226 }
227-
228- // UIToolBar
229- UIToolbar *toolBarAppearance = [UIToolbar appearance ];
230- toolBarAppearance.barTintColor = ToolBarBarTintColor;
231- [toolBarAppearance setBackgroundImage: ToolBarBackgroundImage forToolbarPosition: UIBarPositionAny barMetrics: UIBarMetricsDefault];
232-
233- UIColor *toolbarShadowImageColor = ToolBarShadowImageColor;
234- if (toolbarShadowImageColor) {
235- [toolBarAppearance setShadowImage: [UIImage qmui_imageWithColor: toolbarShadowImageColor size: CGSizeMake (1 , PixelOne) cornerRadius: 0 ] forToolbarPosition: UIBarPositionAny];
227+ }
228+
229+ - (void )setNavBarTitleColor : (UIColor *)navBarTitleColor {
230+ _navBarTitleColor = navBarTitleColor;
231+ if (self.navBarTitleFont || self.navBarTitleColor ) {
232+ NSMutableDictionary <NSString *, id > *titleTextAttributes = [[NSMutableDictionary alloc ] init ];
233+ if (self.navBarTitleFont ) {
234+ [titleTextAttributes setValue: self .navBarTitleFont forKey: NSFontAttributeName ];
235+ }
236+ if (self.navBarTitleColor ) {
237+ [titleTextAttributes setValue: self .navBarTitleColor forKey: NSForegroundColorAttributeName ];
238+ }
239+ [UINavigationBar appearance ].titleTextAttributes = titleTextAttributes;
236240 }
237-
238- // UITabBar
239- UITabBar *tabBarAppearance = [UITabBar appearance ];
240- tabBarAppearance.barTintColor = TabBarBarTintColor;
241- tabBarAppearance.backgroundImage = TabBarBackgroundImage;
242- UIColor *tabBarShadowImageColor = TabBarShadowImageColor;
243- if (tabBarShadowImageColor) {
244- [tabBarAppearance setShadowImage: [UIImage qmui_imageWithColor: tabBarShadowImageColor size: CGSizeMake (1 , PixelOne) cornerRadius: 0 ]];
241+ }
242+
243+ - (void )setNavBarBackIndicatorImage : (UIImage *)navBarBackIndicatorImage {
244+ _navBarBackIndicatorImage = navBarBackIndicatorImage;
245+
246+ if (_navBarBackIndicatorImage) {
247+ UINavigationBar *navBarAppearance = [UINavigationBar appearance ];
248+
249+ // 返回按钮的图片frame是和系统默认的返回图片的大小一致的(13, 21),所以用自定义返回箭头时要保证图片大小与系统的箭头大小一样,否则无法对齐
250+ CGSize systemBackIndicatorImageSize = CGSizeMake (13 , 21 ); // 在iOS9上实际测量得到
251+ CGSize customBackIndicatorImageSize = _navBarBackIndicatorImage.size ;
252+ if (!CGSizeEqualToSize (customBackIndicatorImageSize, systemBackIndicatorImageSize)) {
253+ CGFloat imageExtensionVerticalFloat = CGFloatGetCenter (systemBackIndicatorImageSize.height , customBackIndicatorImageSize.height );
254+ _navBarBackIndicatorImage = [_navBarBackIndicatorImage qmui_imageWithSpacingExtensionInsets: UIEdgeInsetsMake (imageExtensionVerticalFloat,
255+ 0 ,
256+ imageExtensionVerticalFloat,
257+ systemBackIndicatorImageSize.width - customBackIndicatorImageSize.width)];
258+ }
259+
260+ navBarAppearance.backIndicatorImage = _navBarBackIndicatorImage;
261+ navBarAppearance.backIndicatorTransitionMaskImage = navBarAppearance.backIndicatorImage ;
245262 }
263+ }
264+
265+ - (void )setNavBarBackButtonTitlePositionAdjustment : (UIOffset)navBarBackButtonTitlePositionAdjustment {
266+ _navBarBackButtonTitlePositionAdjustment = navBarBackButtonTitlePositionAdjustment;
246267
247- // UITabBarItem
248- UITabBarItem *tabBarItemAppearance = [UITabBarItem appearance ];
249-
250- UIColor *tabBarItemTitleColor = TabBarItemTitleColor;
251- if (tabBarItemTitleColor) {
252- [tabBarItemAppearance setTitleTextAttributes: @{NSForegroundColorAttributeName :tabBarItemTitleColor} forState: UIControlStateNormal];
268+ if (!UIOffsetEqualToOffset (UIOffsetZero, _navBarBackButtonTitlePositionAdjustment)) {
269+ UIBarButtonItem *backBarButtonItem = [UIBarButtonItem appearance ];
270+ [backBarButtonItem setBackButtonTitlePositionAdjustment: _navBarBackButtonTitlePositionAdjustment forBarMetrics: UIBarMetricsDefault];
253271 }
254-
255- UIColor *tabBarItemTitleColorSelected = TabBarItemTitleColorSelected;
256- if (tabBarItemTitleColorSelected) {
257- [tabBarItemAppearance setTitleTextAttributes: @{NSForegroundColorAttributeName :tabBarItemTitleColorSelected} forState: UIControlStateSelected];
272+ }
273+
274+ - (void )setToolBarBarTintColor : (UIColor *)toolBarBarTintColor {
275+ _toolBarBarTintColor = toolBarBarTintColor;
276+ [UIToolbar appearance ].barTintColor = _toolBarBarTintColor;
277+ }
278+
279+ - (void )setToolBarBackgroundImage : (UIImage *)toolBarBackgroundImage {
280+ _toolBarBackgroundImage = toolBarBackgroundImage;
281+ [[UIToolbar appearance ] setBackgroundImage: _toolBarBackgroundImage forToolbarPosition: UIBarPositionAny barMetrics: UIBarMetricsDefault];
282+ }
283+
284+ - (void )setToolBarShadowImageColor : (UIColor *)toolBarShadowImageColor {
285+ _toolBarShadowImageColor = toolBarShadowImageColor;
286+ if (_toolBarShadowImageColor) {
287+ [[UIToolbar appearance ] setShadowImage: [UIImage qmui_imageWithColor: _toolBarShadowImageColor size: CGSizeMake (1 , PixelOne) cornerRadius: 0 ] forToolbarPosition: UIBarPositionAny];
288+ }
289+ }
290+
291+ - (void )setToolBarButtonFont : (UIFont *)toolBarButtonFont {
292+ _toolBarButtonFont = toolBarButtonFont;
293+ if (_toolBarButtonFont) {
294+ UIBarButtonItem *barButtonItemAppearance = [UIBarButtonItem appearance ];
295+ [barButtonItemAppearance setTitleTextAttributes: @{NSFontAttributeName : _toolBarButtonFont} forState: UIControlStateNormal];
296+ }
297+ }
298+
299+ - (void )setTabBarBarTintColor : (UIColor *)tabBarBarTintColor {
300+ _tabBarBarTintColor = tabBarBarTintColor;
301+ [UITabBar appearance ].barTintColor = _tabBarBarTintColor;
302+ }
303+
304+ - (void )setTabBarBackgroundImage : (UIImage *)tabBarBackgroundImage {
305+ _tabBarBackgroundImage = tabBarBackgroundImage;
306+ [UITabBar appearance ].backgroundImage = _tabBarBackgroundImage;
307+ }
308+
309+ - (void )setTabBarShadowImageColor : (UIColor *)tabBarShadowImageColor {
310+ _tabBarShadowImageColor = tabBarShadowImageColor;
311+ if (_tabBarShadowImageColor) {
312+ [[UITabBar appearance ] setShadowImage: [UIImage qmui_imageWithColor: _tabBarShadowImageColor size: CGSizeMake (1 , PixelOne) cornerRadius: 0 ]];
313+ }
314+ }
315+
316+ - (void )setTabBarItemTitleColor : (UIColor *)tabBarItemTitleColor {
317+ _tabBarItemTitleColor = tabBarItemTitleColor;
318+ if (_tabBarItemTitleColor) {
319+ [[UITabBarItem appearance ] setTitleTextAttributes: @{NSForegroundColorAttributeName : _tabBarItemTitleColor} forState: UIControlStateNormal];
320+ }
321+ }
322+
323+ - (void )setTabBarItemTitleColorSelected : (UIColor *)tabBarItemTitleColorSelected {
324+ _tabBarItemTitleColorSelected = tabBarItemTitleColorSelected;
325+ if (_tabBarItemTitleColorSelected) {
326+ [[UITabBarItem appearance ] setTitleTextAttributes: @{NSForegroundColorAttributeName : _tabBarItemTitleColorSelected} forState: UIControlStateSelected];
258327 }
259328}
260329
0 commit comments