设置导航栏标题的文字属性
属性 设置 文字 导航 标题
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。
相关文章
- SpriteBuilder中不能编辑自定义类或不能给节点添加属性的解决
- [Web前端] 给li设置float浮动属性之后,无法撑开外层ul的问题。
- c# 属性类(特性)
- json序列化时忽略属性设置
- [HTML/CSS]margin属性用法
- SAP Spartacus cxFocus的config属性的赋值原理
- SAP Spartacus User form属性的运行时字段明细
- Angular应用Base Element Href属性的设置
- iOS swift 设置电量 计算属性实例
- Android 10.0 app全屏系统属性控制上滑是否显示虚拟导航栏和状态栏
- 第47篇 C++嵌入QML中 之 设置简单的上下文属性
- PowerDesigner 将表的字段name属性设置到comment凝视
- 浅析Web API中FromBody属性
- 【CSS】盒子模型外边距 ① ( 盒子模型外边距设置 | 外边距属性单独设置 | 外边距属性复合写法 )
- 【Groovy】使用 SwingBuilder 构建 Swing 窗口 ( 创建一个 Swing 构造器 | 配置 Swing 窗口的一系列属性 | 设置 Swing 窗口显示 )
- 【Groovy】循环控制 ( Java 语法循环 | 默认的 IntRange 构造函数 | 可设置翻转属性的 IntRange 构造函数 | 可设置是否包含 to 的构造函数 | 0..9 简写 )
- 【Android 逆向】Android 权限 ( ro.product.cpu.abi 属性 | ro.zygote 属性 | dhcp.eth0 属性 | net.* 属性 )
- 【Android 内存优化】自定义组件长图组件 ( 获取图像宽高 | 计算解码区域 | 设置图像解码属性 复用 像素格式 | 图像绘制 )
- vue获取DOM元素并设置属性
- HTML DOM中innerHTML、textContent和innerText属性的用法与区别
- Groovy中的任务的自定义属性设置
- react是否支持给标签设置自定义的属性,比如给video标签设置webkit-playsinline?
- XML元素和属性
- Android 11.0 系统属性控制sdcard和usb是否挂载的功能实现
- Qt编写控件属性设计器7-串口采集