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

效果:

源码:

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

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

时间: 2024-08-28 18:42:01

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

【转】iOS中设置导航栏标题的字体颜色和大小

原文网址:http://www.360doc.com/content/15/0417/11/20919452_463847404.shtml iOS中设置导航栏标题的字体颜色和大小,有需要的朋友可以参考下. 在平时开发项目的时候,难免会遇到修改导航栏字体大小和颜色的需求,一般使用自定义视图的方法,其实还存在一种方法. 方法一:(自定义视图的方法,一般人也会采用这样的方式) 就是在导航向上添加一个titleView,可以使用一个label,再设置label的背景颜色透明,字体什么的设置就很简单了.

设置导航栏标题颜色及字体大小

//改变导航栏标题颜色及字体大小 e.g设置字体颜色为red 字体大小为18 self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor redColor], NSFontAttributeName : [UIFont boldSystemFontOfSize:18]}; //改变导航栏返回按钮颜色 [self.navigationControll

iOS中设置导航栏标题的字体颜色和大小

在平时开发项目的时候,难免会遇到修改导航栏字体大小和颜色的需求,一般使用自定义视图的方法,其实还存在一种方法. 方法一:(自定义视图的方法,一般人也会采用这样的方式) 就是在导航向上添加一个titleView,可以使用一个label,再设置label的背景颜色透明,字体什么的设置就很简单了. //自定义标题视图 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)]; titleLabel.b

设置导航栏的相关属性

很多其它具体内容请參考http://www.cocoachina.com/applenews/devnews/2013/1104/7287.html 别人的代码敲一遍就成了自己的了 //改动导航栏的背景色 [[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]]; //设置导航栏的背景图片. [[UINavigationBar appearance] setBackgroundImage:[UIImage image

iOS 设置导航栏之二(设置导航栏的颜色、文字的颜色、左边按钮的文字及颜色)

                  #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end #import "AppDelegate.h" #import "KeyViewController.h" @interface Ap

Swift 设置导航栏透明

Swift 设置导航栏透明,就是设置一张空的图片,但如果只设置这一句代码,并不起作用,还需要其他设置,直接上代码 // 1.设置导航栏标题属性:设置标题颜色 self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white] // 2.设置导航栏前景色:设置item指示色 self.navigationController?.navigation

iOS设置导航栏样式(UINavigationController)

//设置导航栏baritem和返回baiitem样式 UIBarButtonItem *barItem = [UIBarButtonItem appearance]; //去掉返回按钮上的字 [barItem setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault]; //把按钮设置为白色 barItem.tintColor = [UIColor whiteColor]

ios weibo 第二天 设置导航栏属性,添加加号按钮

要点:1.在底部添加加号按钮 2.设置导航栏属性 1.weibo底部的button其中四个按钮是一样的,其中中间的加号需要另外做处理 tablebar是自己定义的 ,代码如下 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // 删除系统自动生成的UITabBarButton for (UIView *child in self.tabBar.subviews) { if ([child isKin

iOS 设置导航栏的颜色和导航栏上文字的颜色

#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end #import "AppDelegate.h" #import "KeyViewController.h" @interface AppDelegate () @end