博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设置导航栏标题的文字属性
阅读量:7028 次
发布时间:2019-06-28

本文共 3484 字,大约阅读时间需要 11 分钟。

设置导航栏标题的文字属性

效果:

源码:

UINavigationController+TitleTextAttributes.h 与 UINavigationController+TitleTextAttributes.m

////  UINavigationController+TitleTextAttributes.h//  NC////  Copyright (c) 2014年 Y.X. All rights reserved.//#import 
@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 
@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来实现

一个这么简单的功能为何要这么折腾?其实这就是提高效率的方案,将重复代码抽象成类,你不用再去关注复制粘贴代码,还不懂细节的含义,而是你可以见名知意一目了然而已。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:http://idrxl.baihongyu.com/

你可能感兴趣的文章
区块链— 比特币中的区块、账户验证和记账
查看>>
Electron打包,NSIS修改默认安装路径
查看>>
分享一些好用的网站
查看>>
【Android】Retrofit 2.0 的使用
查看>>
Nacos系列:基于Nacos的注册中心
查看>>
原生JS 实现复杂对象深拷贝(对象值包含函数)
查看>>
【跃迁之路】【732天】程序员高效学习方法论探索系列(实验阶段489-2019.2.22)...
查看>>
PAT A1060 科学记数法经典例题(全string库解决)
查看>>
仿知乎分享界面
查看>>
最小外接矩形思路以及实现
查看>>
Python是什么?简单了解pythonp-入门
查看>>
利用ES6进行Promise封装总结
查看>>
ES10 特性的完整指南
查看>>
学习threejs走过的坑
查看>>
ThinkSNS+的 SPA(H5)安装教程
查看>>
C++回声服务器_5-多进程版本
查看>>
CSS外边距折叠引发的问题
查看>>
【LeetCode】贪心算法--划分字母区间(763)
查看>>
Android 抖音爆红的口红挑战爬坑总结
查看>>
怎么就没发现华为Mate20 pro有这么多神奇功能!这波黑科技盘它!
查看>>