自定义按钮(文字和图标)
2015-11-03 12:07
246 查看
自定义按钮,即自定义按钮中的文字和图标。
自定义一个继承自
代码如下:
自定义一个继承自
UIButton的类,重写父类的一些方法:
代码如下:
.h文件
#import <UIKit/UIKit.h> @interface SStiBtn : UIButton @end
.m文件
#import "SStiBtn.h" @implementation SStiBtn -(instancetype)initWithFrame:(CGRect)frame{ self=[super initWithFrame:frame]; if (self) { self.imageView.contentMode=UIViewContentModeCenter; self.titleLabel.textAlignment=NSTextAlignmentRight ; [self.titleLabel setTextColor:[UIColor purpleColor]]; } return self; } //设置内部图标的frame -(CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat imageY=0; CGFloat imageW=self.height; CGFloat imageH=self.height; CGFloat imageX=self.width-imageW; return CGRectMake(imageX, imageY, imageW, imageH); } //设置内部文字的frame -(CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat titleX=0; CGFloat titleY=0; CGFloat titleH=self.height; CGFloat titleW=self.width-self.imageView.width; return CGRectMake(titleX, titleY, titleW, titleH); } @end
相关文章推荐
- LeetCode Add and Search Word - Data structure design
- 软件工程和计算机科学与技术的区别
- Pig error : mismatched input 'Map' expecting EOF
- 截取HTML中代码块生成图片
- 图片翻页
- 权限管理系统之角色管理
- Hide-Music-Player 一个完整的音乐播放器《IT蓝豹》
- 自定义toast
- ul 居中方法(兼容 IE)
- #ifdef __cplusplus代码段
- 乐视手机1S正式发售,乐视商城官网抽风遭网友吐槽
- Apache-Tika解析PDF文档
- Matlab 三维图像绘制(1)——纹理贴图、球体
- UITextField监听文字输入事件
- JDBC:Apache DBUtils的使用
- JVM调优总结(八)-典型配置举例2
- 全排列 Permutations
- Microsoft Visual Studio 2010下如何添加命令提示行
- Jfire-Core 框架API文档
- 探寻阿里云服务器迈入2.0时代的技术要点