首页 > 代码库 > 设置导航栏标题的文字属性
设置导航栏标题的文字属性
设置导航栏标题的文字属性
效果:
源码:
UINavigationController+TitleTextAttributes.h 与 UINavigationController+TitleTextAttributes.m
//// UINavigationController+TitleTextAttributes.h// NC//// Copyright (c) 2014年 Y.X. All rights reserved.//#import <UIKit/UIKit.h>@class NCTitleAttribute;@interface UIViewController (TitleTextAttributes)- (void)titleTextAttributes:(NCTitleAttribute *)attribute;@end
//// UINavigationController+TitleTextAttributes.m// NC//// Copyright (c) 2014年 Y.X. All rights reserved.//#import "UINavigationController+TitleTextAttributes.h"#import "NCTitleAttribute.h"@implementation UIViewController (TitleTextAttributes)#pragma mark - public- (void)titleTextAttributes:(NCTitleAttribute *)attribute{ [self controller:self titleTextAttributes:[attribute transformToDictionary]];}#pragma mark - private- (void)controller:(UIViewController *)controller titleTextAttributes:(NSDictionary *)dictionary{ if ([controller isKindOfClass:[UIViewController class]]) { [controller.navigationController.navigationBar setTitleTextAttributes:dictionary]; }}@end
NCTitleAttribute.h 与 NCTitleAttribute.m
//// NCTitleAttribute.h// NC//// Copyright (c) 2014年 Y.X. All rights reserved.//#import <Foundation/Foundation.h>@interface NCTitleAttribute : NSObject@property (nonatomic, strong) UIColor *titleColor; // 标题颜色@property (nonatomic, strong) UIFont *titleFont; // 标题字体@property (nonatomic, strong) UIColor *shadowColor; // 阴影颜色@property (nonatomic, assign) CGSize shadowOffset; // 阴影偏移量// 将参数转换为字典- (NSDictionary *)transformToDictionary;@end
//// NCTitleAttribute.m// NC//// Copyright (c) 2014年 Y.X. All rights reserved.//#import "NCTitleAttribute.h"@implementation NCTitleAttribute- (NSDictionary *)transformToDictionary{ NSMutableDictionary *dic = [NSMutableDictionary new]; if (_titleColor) { [dic setObject:_titleColor forKey:NSForegroundColorAttributeName]; } else { [dic setObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName]; } if (_titleFont) { [dic setObject:_titleFont forKey:NSFontAttributeName]; } if (_shadowOffset.height || _shadowOffset.width) { NSShadow *shadow = [NSShadow new]; shadow.shadowColor = _shadowColor; shadow.shadowOffset = _shadowOffset; [dic setObject:shadow forKey:NSShadowAttributeName]; } return dic;}@end
使用的源码:
//// RootViewController.m// NC//// Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"#import "UINavigationController+TitleTextAttributes.h"#import "NCTitleAttribute.h"#import "FontPool.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; [FontPool registerFont:bundleFont(@"华康少女字体.ttf") withName:@"华康少女字体"]; // 设置导航栏标题 self.title = @"YouXianMing"; NCTitleAttribute *titleAttribute = [NCTitleAttribute new]; titleAttribute.titleColor = [UIColor redColor]; titleAttribute.titleFont = [UIFont fontWithName:CUSTOM_FONT(@"华康少女字体", 0) size:20.f]; titleAttribute.shadowColor = [UIColor blackColor]; titleAttribute.shadowOffset = CGSizeMake(1, 1); [self titleTextAttributes:titleAttribute];}@end
简单的分析:
其实,核心的方法就一个而已
然后,将那个字典抽象成了对象,将复杂的设置转换成了简单的对象来理解
然后,使用的时候是通过category来实现
一个这么简单的功能为何要这么折腾?其实这就是提高效率的方案,将重复代码抽象成类,你不用再去关注复制粘贴代码,还不懂细节的含义,而是你可以见名知意一目了然而已。
设置导航栏标题的文字属性
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。