zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

属性 设置 文字 导航 标题
2023-09-14 08:57:17 时间

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

效果:

源码:

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 ()

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

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

 


网页标题设置,为什么在SERP中,显示结果不一致? 在网站建设与运营的过程中,我们经常会遇到各种各样的问题,特别是关于网页标题设置的问题,如果一个页面标题出错,那么,你整个页面建设的过程,就完全是事倍功半,得不偿失。
所谓的标题配置错误,不见得就是你在自身网站web页面中显示的标题配置错误,而是在相对应网页模板中title标签配置错误,基于这个原因,通常,搜索引擎都会识别 title /title 标签中的标题,而非是站内显示的标题。
HTMLCSS实现左侧固定宽度右侧内容可滚动 在做移动端页面的时候,经常会碰到一个div中分左右两个div,左侧div固定宽度或百分比,右侧div中内容左右溢出,需要左右滑动才可以浏览到全部内容,为此写了一个demo。 处理这个问题的核心关键点是右侧div需要设置固定宽度或者百分比,然后设置它的overflow为auto或者scroll。