DockBtn.h
//
// DockBtn.h
// 20_帅哥no微博
//
// Created by beyond on 14-8-4.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// 一个DockBtn代表Dock上面的一个按钮,它有个成员是对应子控制器的类名,比如Home按钮,成员属性的值就是叫:HomeViewController
#import <UIKit/UIKit.h>
@interface DockBtn : UIButton
// 每个dockBtn中,用一个成员记住 它对应的控制器的类名
@property (nonatomic,copy) NSString *viewControllerClassName;
@end
DockBtn.m
//
// DockBtn.m
// 20_帅哥no微博
//
// Created by beyond on 14-8-4.
// Copyright (c) 2014年 com.beyond. All rights reserved.
// 一个DockBtn代表Dock上面的一个按钮,它有个成员是对应子控制器的类名,比如Home按钮,成员属性的值就是叫:HomeViewController
#import "DockBtn.h"
// 按钮的内容的总宽度
#define kBtnContentWidth contentRect.size.width
// 按钮的内容的总高度
#define kBtnContentHeight contentRect.size.height
// 按钮里的图片的所占的高度比例
#define kImageHeightRatio 0.6
// 按钮里的文本标签的所占的高度比例
#define kLabelHeightRatio (1- kImageHeightRatio)
@implementation DockBtn
// 一些默认的通用的属性一定要写在构造方法里面
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 1.设置按钮文字属性 (局中,字体大小)
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.font = [UIFont systemFontOfSize:12];
// 2.设置按钮图片属性 (放大模式,取消按钮默认的点击高亮时的变色)
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
// 取消按钮默认的点击高亮时的变色(image is drawn darker when highlighted or pressed)
self.adjustsImageWhenHighlighted = NO;
// 3.分类方法,设置按钮选中时的背景
[self setBgImgForSelected:@"tabbar_slider.png"];
}
return self;
}
#pragma mark 重写父类的方法(覆盖父类在高亮时所作的行为)
- (void)setHighlighted:(BOOL)highlighted
{
// 因为 这里只需用按钮的选中和默认状态时的图片,所以要取消高亮状态的一些默认变色行为
// 这里什么也不写,即取消,按钮本身 在高亮的时候执行的那些行为
}
#pragma mark 返回是按钮内部UIImageView的边框(按钮中的图片在上方,居中)
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
// 要居中,最快办法就是让按钮中的图片宽度和按钮一样宽
return CGRectMake(0, 0, kBtnContentWidth, kBtnContentHeight * kImageHeightRatio);
}
#pragma mark 返回是按钮内部UILabel的边框(按钮中的文字在下方,居中)
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
// 要居中,最快办法就是让按钮中的Label宽度和按钮一样宽
// 文字的y位于图片的下边线的上方5个单位距离,即距离图片上方5
CGFloat labelY = kBtnContentHeight * kImageHeightRatio - 5;
// 文字的高度是占按钮余下的所有高度
CGFloat labelHeight = kBtnContentHeight - labelY;
return CGRectMake(0, labelY, kBtnContentWidth, labelHeight);
}
@end










