您的位置:首页 > 移动开发 > IOS开发

ios-新浪微博开发-22-封装标题按钮

2015-09-20 21:51 495 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: