#import "QHTitleButton.h"
@implementation QHTitleButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// self.imageView.contentMode = UIViewContentModeCenter;
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
self.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[self setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];
}
return self;
}
/**
* 设置按钮内部的imageView的frame
*
* @param contentRect 按钮的bounds
*
*/
//- (CGRect)imageRectForContentRect:(CGRect)contentRect
//{
// CGFloat x = 80;
// CGFloat y = 0;
// CGFloat width = 13;
// CGFloat height = contentRect.size.height;
//
// return CGRectMake(x, y, width, height);
//}
///**
// * 设置按钮内部的titleLabel的frame
// *
// * @param contentRect 按钮的bounds
// *
// */
//- (CGRect)titleRectForContentRect:(CGRect)contentRect
//{
// CGFloat x = 0;
// CGFloat y = 0;
// CGFloat width = 80;
// CGFloat height = contentRect.size.height;
//
// return CGRectMake(x, y, width, height);
//}
- (void)layoutSubviews
{
[super layoutSubviews];
//如果仅仅是调整按钮内部 titleLabel imageView 的位置 那么我们在layoutSubView中单独设置位置即可
//1.计算lable 的frame
self.titleLabel.x = self.imageView.x;
self.imageView.x = CGRectGetMaxX(self.titleLabel.frame);
// NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
// attrs[NSFontAttributeName] = self.titleLabel.font;
// CGFloat titleW = [self.currentTitle sizeWithAttributes:attrs].width;
//2.计算imageView frame
}
#warning 重写系统内部方法 必须先调用父类的方法
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
[super setTitle:title forState:state];
[self sizeToFit];
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
[super setImage:image forState:state];
[self sizeToFit];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
ios-新浪微博开发-22-封装标题按钮
最新推荐文章于 2020-11-20 10:21:21 发布