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
相关文章推荐
- ios-新浪微博开发-21-获取用户昵称
- ios-新浪微博开发20-控制器的切换
- ios-新浪微博开发19-账号存储工具类
- ios-新浪微博开发-18-(授权存储账号信息)用归档实现
- iOS 中常用的四种数据持久化的方法
- iOS ARC简要介绍
- iOS崩溃堆栈符号化,定位问题分分钟搞定!
- Cordova-iOS自定义插件以及和老版本的差异
- iOS开发小白学习体验-9
- 添加图片水印
- iOS项目开发实战——使用CALayer实现图片的淡入淡出效果
- 关于 iOS 程序的内存分析
- Windows下搭建IOS开发环境(一)
- 蓝懿iOS培训日志16 字典
- iOS项目开发实战——使用CALayer和定时器实现进度条
- iOS-常用技巧
- BitMap类型的图形上下文
- iOS项目开发实战——使用定时器实现循环操作
- SSH连接IOS越狱设备
- Xcode绘制文字及图片