版本记录
版本号 | 时间 |
---|---|
V1.0 | 2017.08.04 |
前言
我们的app一般都需要导航栏,极个别的除外,比如说很多游戏是没有导航栏的,但是绝大部分是都有的,只要有导航栏就需要显示主题,左右
item
的图片或者文字,并且需要处理相关的事件,这里我就简单的封装了一个UIBarButtonItem
的分类,可以快速的实现左右item
图片和文字的显示,并预留了相关接口。整个框架只依赖苹果的API,不依赖其他第三方,拖入即可使用,简单方便。
预先了解
我这里写的是UIBarButtonItem
的分类,可以实现导航左右item
的文字以及图片的显示,并可响应相应的方法。
框架实现
下面我们就看一下所写框架的全部代码。
1. UIBarButtonItem+JJBarButtonItem.h
#import <UIKit/UIKit.h>
@interface UIBarButtonItem (JJBarButtonItem)
/**
* 该函数的功能返回导航的leftItem
*
* @param target :目标
* @param selector :响应方法
* @param imageNormal :正常的图片
* @param imageHightlighted :高亮的图片
* @param titleColorNormal :正常文字颜色
* @param titleColorHightlighted :高亮文字颜色
* @param title :文字
*
* @return 左边UIBarButtonItem
*/
+ (UIBarButtonItem *)jjLeftItem:(id)target
selector:(SEL)selector
imageNormal:(UIImage *)imageNormal
imageHightlighted:(UIImage *)imageHightlighted
titleColorNormal:(UIColor *)titleColorNormal
titleColorHightlighted:(UIColor *)titleColorHightlighted
title:(NSString *)title;
/**
* 该函数的功能返回导航的rightItem
*
* @param target :目标
* @param selector :响应方法
* @param imageNormal :正常的图片
* @param imageHightlighted :高亮的图片
* @param imageDisabled :无法点击的图片
* @param titleColorNormal :正常文字颜色
* @param titleColorHightlighted :高亮文字颜色
* @param titleColorDisabled :无法点击的文字
* @param title :文字
*
* @return 右边UIBarButtonItem
*/
+ (UIBarButtonItem *)jjRightItem:(id)target
selector:(SEL)selector
imageNormal:(UIImage *)imageNormal
imageHightlighted:(UIImage *)imageHightlighted
imageDisabled:(UIImage *)imageDisabled
titleColorNormal:(UIColor *)titleColorNormal
titleColorHightlighted:(UIColor *)titleColorHightlighted
titleColorDisabled:(UIColor *)titleColorDisabled
title:(NSString *)title;
//几种简化默认返回简化写法
+ (UIBarButtonItem *)jjDefaultBackItem:(id)target
selector:(SEL)selector
title:(NSString *)title;
+ (UIBarButtonItem *)jjDefaultBackItem:(id)target
selector:(SEL)selector
titleColorNormal:(UIColor *)titleColorNormal
titleColorHightlighted:(UIColor *)titleColorHightlighted
title:(NSString *)title;
+ (UIBarButtonItem *)jjDefaultRightTitleItem:(id)target
selector:(SEL)selector
title:(NSString *)title;
+ (UIBarButtonItem *)jjDefaultRightItem:(id)target
selector:(SEL)selector
titleColorNormal:(UIColor *)titleColorNormal
titleColorHightlighted:(UIColor *)titleColorHightlighted
titleColorDisabled:(UIColor *)titleColorDisabled
title:(NSString *)title;
+ (UIBarButtonItem *)jjDefaultRightImageItem:(id)target
selector:(SEL)selector
imageNormal:(UIImage *)imageNormal
imageHightlighted:(UIImage *)imageHightlighted
imageDisabled:(UIImage *)imageDisabled;
- (UIButton *)jjContentViewButton;
@end
2. UIBarButtonItem+JJBarButtonItem.m
#import "UIBarButtonItem+JJBarButtonItem.h"
#define kDefaultColor ([UIColor whiteColor])
#define kLeftTitleFont (17.0)
#define kRightTitleFont (17.0)
@implementation UIBarButtonItem (JJBarButtonItem)
+ (UIBarButtonItem *)jjLeftItem:(id)target
selector:(SEL)selector
imageNormal:(UIImage *)imageNormal
imageHightlighted:(UIImage *)imageHightlighted
titleColorNormal:(UIColor *)titleColorNormal
titleColorHightlighted:(UIColor *)titleColorHightlighted
title:(NSString *)title
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor clearColor];
[btn setImage:imageNormal forState:UIControlStateNormal];
[btn setImage:imageHightlighted?:imageNormal forState:UIControlStateHighlighted];
[btn setTitleColor:titleColorNormal forState:UIControlStateNormal];
[btn setTitleColor:titleColorHightlighted?:titleColorNormal forState:UIControlStateHighlighted];
[btn setTitle:title forState:UIControlStateNormal];
btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
btn.titleLabel.font = [UIFont systemFontOfSize:kLeftTitleFont];
// 导航栏高度
CGFloat fNaviBarHeight = 44.0;
// 图片宽度
CGFloat fImageWidth = 0.0;
if (imageNormal) {
fImageWidth = imageNormal.size.width;
}
// 文本宽度
CGFloat fTitleWidth = 0.0;
if (title.length > 0) {
fTitleWidth = [self jjBoundingSizeWithLabel:btn.titleLabel size:CGSizeMake([UIScreen mainScreen].bounds.size.width, fNaviBarHeight)].width;
}
// 图片、文本间隔
CGFloat fImageAndTitleEdge = 5.0;
CGFloat fMaxWidth = fminf(fImageWidth + fTitleWidth + fImageAndTitleEdge, [UIScreen mainScreen].bounds.size.width * 0.3);
btn.frame = CGRectMake(0.0, 0.0, fMaxWidth, fNaviBarHeight);
// 文本偏移
CGFloat fLeftEdgeInsets = 5.0;
if (fTitleWidth > 0.0) {
btn.titleEdgeInsets = UIEdgeInsetsMake(0.0, -fLeftEdgeInsets, 0.0, fMaxWidth + fLeftEdgeInsets - fTitleWidth);
}
// 图片偏移
if (fImageWidth > 0.0) {
btn.imageEdgeInsets = UIEdgeInsetsMake(0.0, -fLeftEdgeInsets, 0.0, fMaxWidth + fLeftEdgeInsets - fImageWidth);
btn.titleEdgeInsets = UIEdgeInsetsMake(0.0, fImageAndTitleEdge - fLeftEdgeInsets, 0.0, fLeftEdgeInsets);
}
btn.exclusiveTouch = YES;
[btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
return item;
}
+ (UIBarButtonItem *)jjRightItem:(id)target
selector:(SEL)selector
imageNormal:(UIImage *)imageNormal
imageHightlighted:(UIImage *)imageHightlighted
imageDisabled:(UIImage *)imageDisabled
titleColorNormal:(UIColor *)titleColorNormal
titleColorHightlighted:(UIColor *)titleColorHightlighted
titleColorDisabled:(UIColor *)titleColorDisabled
title:(NSString *)title
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor clearColor];
[btn setImage:imageNormal forState:UIControlStateNormal];
[btn setImage:imageHightlighted?:imageNormal forState:UIControlStateHighlighted];
[btn setImage:imageDisabled?:imageNormal forState:UIControlStateDisabled];
[btn setTitleColor:titleColorNormal forState:UIControlStateNormal];
[btn setTitleColor:titleColorHightlighted?:titleColorNormal forState:UIControlStateHighlighted];
[btn setTitleColor:titleColorDisabled?:titleColorNormal forState:UIControlStateDisabled];
[btn setTitle:title forState:UIControlStateNormal];
btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
btn.titleLabel.font = [UIFont systemFontOfSize:kRightTitleFont];
// 导航栏高度
CGFloat fNaviBarHeight = 44.0;
// 图片宽度
CGFloat fImageWidth = 0.0;
if (imageNormal) {
fImageWidth = imageNormal.size.width;
}
// 文本宽度
CGFloat fTitleWidth = 0.0;
if (title.length > 0) {
fTitleWidth = [self jjBoundingSizeWithLabel:btn.titleLabel size:CGSizeMake([UIScreen mainScreen].bounds.size.width, fNaviBarHeight)].width;
}
// 图片、文本间隔
CGFloat fImageAndTitleEdge = 5.0;
CGFloat fMaxWidth = fminf(fmaxf(fImageWidth + fTitleWidth + fImageAndTitleEdge, 44.0), [UIScreen mainScreen].bounds.size.width * 0.3);
btn.frame = CGRectMake(0.0, 0.0, fMaxWidth, fNaviBarHeight);
// 图片偏移
CGFloat fRightEdgeInsets = 2.0;
if (fImageWidth > 0.0) {
btn.imageEdgeInsets = UIEdgeInsetsMake(0.0, fMaxWidth + fRightEdgeInsets - fImageWidth, 0.0, -fRightEdgeInsets);
}
// 文本偏移
if (fTitleWidth > 0.0) {
btn.imageEdgeInsets = UIEdgeInsetsZero;
btn.titleEdgeInsets = UIEdgeInsetsMake(0.0, fRightEdgeInsets + fImageAndTitleEdge, 0.0, -fRightEdgeInsets);
}
btn.exclusiveTouch = YES;
[btn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
return item;
}
+ (UIBarButtonItem *)jjDefaultBackItem:(id)target
selector:(SEL)selector
title:(NSString *)title
{
return [UIBarButtonItem jjLeftItem:target
selector:selector
imageNormal:[UIImage imageNamed:@"default_back_normal"]
imageHightlighted:[UIImage imageNamed:@"default_back_hightlighted"]
titleColorNormal:[self jjColorWithHexString:@"#3e3e3e" alpha:1.0]
titleColorHightlighted:[self jjColorWithHexString:@"#858585" alpha:1.0]
title:title];
}
+ (UIBarButtonItem *)jjDefaultBackItem:(id)target
selector:(SEL)selector
titleColorNormal:(UIColor *)titleColorNormal
titleColorHightlighted:(UIColor *)titleColorHightlighted
title:(NSString *)title
{
return [UIBarButtonItem jjLeftItem:target
selector:selector
imageNormal:[UIImage imageNamed:@"default_back_normal"]
imageHightlighted:[UIImage imageNamed:@"default_back_hightlighted"]
titleColorNormal:titleColorNormal
titleColorHightlighted:titleColorHightlighted
title:title];
}
+ (UIBarButtonItem *)jjDefaultRightTitleItem:(id)target
selector:(SEL)selector
title:(NSString *)title
{
return [UIBarButtonItem jjRightItem:target
selector:selector
imageNormal:nil
imageHightlighted:nil
imageDisabled:nil
titleColorNormal:[self jjColorWithHexString:@"#3e3e3e" alpha:1.0]
titleColorHightlighted:[self jjColorWithHexString:@"#858585" alpha:1.0]
titleColorDisabled:nil
title:title];
}
+ (UIBarButtonItem *)jjDefaultRightItem:(id)target
selector:(SEL)selector
titleColorNormal:(UIColor *)titleColorNormal
titleColorHightlighted:(UIColor *)titleColorHightlighted
titleColorDisabled:(UIColor *)titleColorDisabled
title:(NSString *)title
{
return [UIBarButtonItem jjRightItem:target
selector:selector
imageNormal:nil
imageHightlighted:nil
imageDisabled:nil
titleColorNormal:titleColorNormal
titleColorHightlighted:titleColorHightlighted
titleColorDisabled:titleColorDisabled
title:title];
}
+ (UIBarButtonItem *)jjDefaultRightImageItem:(id)target
selector:(SEL)selector
imageNormal:(UIImage *)imageNormal
imageHightlighted:(UIImage *)imageHightlighted
imageDisabled:(UIImage *)imageDisabled
{
return [UIBarButtonItem jjRightItem:target
selector:selector
imageNormal:imageNormal
imageHightlighted:imageHightlighted
imageDisabled:imageDisabled
titleColorNormal:nil
titleColorHightlighted:nil
titleColorDisabled:nil
title:nil];
}
- (UIButton *)jjContentViewButton
{
if ([self.customView isKindOfClass:[UIButton class]]) {
return (UIButton *)(self.customView);
}
return nil;
}
/**
* 该函数的功能返回给定label字符串占的size
* @param label :字符串的label
* @param size :字符串占的size
*/
+ (CGSize)jjBoundingSizeWithLabel:(UILabel *)label size:(CGSize)size
{
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin;
options |= NSStringDrawingUsesFontLeading;
options |= NSStringDrawingTruncatesLastVisibleLine;
CGRect rect = [label.text boundingRectWithSize:size
options:options
attributes:@{NSFontAttributeName:label.font}
context:nil];
// 加两个像素, 防止emoji被切掉
return CGSizeMake(ceilf(rect.size.width) + 1.0, ceilf(rect.size.height) + 2.0);
}
/**
* 该函数的功能就是根据给的十六进制数和透明度返回对应的颜色值
* @param stringToConvert :十六进制字符串
* @param alpha :透明度
*/
+ (UIColor *)jjColorWithHexString:(NSString *)stringToConvert alpha:(CGFloat)alpha
{
NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([cString length] < 6) {
return kDefaultColor;
}
if ([cString hasPrefix:@"#"]) {
cString = [cString substringFromIndex:1];
}
if ([cString length] != 6) {
return kDefaultColor;
}
NSRange range = NSMakeRange(0, 2);
NSString *rString = [cString substringWithRange:range];
range.location = 2;
NSString *gString = [cString substringWithRange:range];
range.location = 4;
NSString *bString = [cString substringWithRange:range];
unsigned int red = 0, green = 0, blue = 0;
if ([[NSScanner scannerWithString:rString] scanHexInt:&red] &&
[[NSScanner scannerWithString:gString] scanHexInt:&green] &&
[[NSScanner scannerWithString:bString] scanHexInt:&blue]) {
return [UIColor colorWithRed:((float)red / 255.0f)
green:((float)green / 255.0f)
blue:((float)blue / 255.0f)
alpha:alpha];
}
return kDefaultColor;
}
@end
别的就不多说了,代码就这么多。
框架验证
下面就验证下框架的使用,我们先写个类调用下试试。
1. JJNavigationVC.m
#import "JJNavigationVC.h"
#import "UIBarButtonItem+JJBarButtonItem.h"
@interface JJNavigationVC ()
@end
@implementation JJNavigationVC
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"无题";
self.view.backgroundColor = [UIColor lightGrayColor];
//左边item
self.navigationItem.leftBarButtonItem = [UIBarButtonItem jjLeftItem:self
selector:@selector(backButtonDidClick)
imageNormal:[UIImage imageNamed:@"my_sezhi1"]
imageHightlighted:[UIImage imageNamed:@"my_sezhi1"]
titleColorNormal:[UIColor blueColor]
titleColorHightlighted:[UIColor redColor]
title:@"返回"
];
//右边的item
self.navigationItem.rightBarButtonItem = [UIBarButtonItem jjRightItem:self
selector:@selector(collectionButtonDidClick)
imageNormal:[UIImage imageNamed:@"my_shoucang1"]
imageHightlighted:[UIImage imageNamed:@"my_shoucang1"]
imageDisabled:[UIImage imageNamed:@"my_shoucang1"]
titleColorNormal:[UIColor blueColor]
titleColorHightlighted:[UIColor redColor]
titleColorDisabled:[UIColor blackColor]
title:@"收藏"
];
}
#pragma mark - Action && Notification
- (void)backButtonDidClick
{
NSLog(@"返回按钮");
}
- (void)collectionButtonDidClick
{
NSLog(@"收藏按钮");
}
@end
下面我们看一下效果展示。
这里文字大小我设置的是17号字体,里面定义的宏,想修改的直接修改宏就可以了。
#define kLeftTitleFont (17.0)
#define kRightTitleFont (17.0)
后记
希望我写的这个小框架能对大家有所帮助,谢谢大家的关注~~,继续努力。