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

简介:

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

效果:

源码:

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来实现

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

 

目录
相关文章
SwiftUI—如何设置导航栏里的标题样式
SwiftUI—如何设置导航栏里的标题样式
769 0
SwiftUI—如何设置导航栏里的标题样式
点击文字显示,点击文字隐藏(3)
点击文字显示,点击文字隐藏(2)
|
7月前
|
JavaScript
点击文字显示,点击文字隐藏(5)
点击文字显示,点击文字隐藏(5)
|
存储 JavaScript
行内表单 在统一行显示搜索框 下拉框 按钮
行内表单 在统一行显示搜索框 下拉框 按钮
行内表单 在统一行显示搜索框 下拉框 按钮
|
JavaScript
使用 jQuery 实现页面背景色的更换,通过下拉框选择对应的颜色,页面背景会随着选中的颜色进行更换
使用 jQuery 实现页面背景色的更换,通过下拉框选择对应的颜色,页面背景会随着选中的颜色进行更换
271 0
使用 jQuery 实现页面背景色的更换,通过下拉框选择对应的颜色,页面背景会随着选中的颜色进行更换
点击element-ui表格中的图标,上方显示具体的文字描述
点击element-ui表格中的图标,上方显示具体的文字描述
点击element-ui表格中的图标,上方显示具体的文字描述
|
搜索推荐 算法
网页标题设置,为什么在SERP中,显示结果不一致?
在网站建设与运营的过程中,我们经常会遇到各种各样的问题,特别是关于网页标题设置的问题,如果一个页面标题出错,那么,你整个页面建设的过程,就完全是事倍功半,得不偿失。 那么,网页标题设置,为什么在SERP中,显示结果不一致? 根据以往标题优化的经验,我们认为,产生这个问题的原因,主要有如下三方面: 1、标题配置错误 所谓的标题配置错误,不见得就是你在自身网站web页面中显示的标题配置错误,而是在相对应网页模板中title标签配置错误,基于这个原因,通常,搜索引擎都会识别<title></title>标签中的标题,而非是站内显示的标题。
1444 0