自定义button同时显示文字和图片
2016-06-20 10:23
323 查看
代码搬运工这样就可以实现图片和文字同时显示
首先创建一个继承于UIButton的类
//1. .h中这样写
#import <UIKit/UIKit.h>
@interface SHJF_Button :
UIButton
@end
//2. .m中这样写
#import "SHJF_Button.h"
@implementation SHJF_Button
重写init方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
//可根据自己的需要随意调整
self .
titleLabel . textAlignment =
NSTextAlignmentLeft ;
self .
titleLabel . font =[
UIFont systemFontOfSize :
14.0 ];
// self . titleLabel . backgroundColor = [UIColor orangeColor];
// self . imageView . contentMode = UIViewContentModeLeft ;
}
return
self;
}
//重写父类UIButton的方法
//更具button的rect设定并返回文本label的rect
- ( CGRect )titleRectForContentRect:(
CGRect )contentRect
{
CGFloat titleW = myWidth/3/5*3
;
CGFloat titleH = myHeight/100*3 ;
CGFloat titleX = myWidth/3/5 ;
CGFloat titleY = myHeight/100*12.4 ;
contentRect = ( CGRect ){{titleX,titleY},{titleW,titleH}};
return contentRect;
}
//更具button的rect设定并返回UIImageView的rect
- ( CGRect )imageRectForContentRect:(
CGRect )contentRect
{
CGFloat imageW = myWidth/100*10 ;
CGFloat imageH = myWidth/100*10 ;
CGFloat imageX = myWidth/100*11.5 ;
CGFloat imageY = myHeight/100*4.1 ;
contentRect = ( CGRect ){{imageX,imageY},{imageW,imageH}};
return contentRect;
}
首先创建一个继承于UIButton的类
//1. .h中这样写
#import <UIKit/UIKit.h>
@interface SHJF_Button :
UIButton
@end
//2. .m中这样写
#import "SHJF_Button.h"
@implementation SHJF_Button
重写init方法
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
//可根据自己的需要随意调整
self .
titleLabel . textAlignment =
NSTextAlignmentLeft ;
self .
titleLabel . font =[
UIFont systemFontOfSize :
14.0 ];
// self . titleLabel . backgroundColor = [UIColor orangeColor];
// self . imageView . contentMode = UIViewContentModeLeft ;
}
return
self;
}
//重写父类UIButton的方法
//更具button的rect设定并返回文本label的rect
- ( CGRect )titleRectForContentRect:(
CGRect )contentRect
{
CGFloat titleW = myWidth/3/5*3
;
CGFloat titleH = myHeight/100*3 ;
CGFloat titleX = myWidth/3/5 ;
CGFloat titleY = myHeight/100*12.4 ;
contentRect = ( CGRect ){{titleX,titleY},{titleW,titleH}};
return contentRect;
}
//更具button的rect设定并返回UIImageView的rect
- ( CGRect )imageRectForContentRect:(
CGRect )contentRect
{
CGFloat imageW = myWidth/100*10 ;
CGFloat imageH = myWidth/100*10 ;
CGFloat imageX = myWidth/100*11.5 ;
CGFloat imageY = myHeight/100*4.1 ;
contentRect = ( CGRect ){{imageX,imageY},{imageW,imageH}};
return contentRect;
}
相关文章推荐
- Mac 查看/关闭 端口占用的方法
- mysql Last_IO_Errno: 1593 报错
- sqlite时间函数及时间日期处理
- hadoop面试题整理(二)(13)
- 深入浅出JMS(一)--JMS基本概念
- android 程序架构分析
- cocos-笔记-判断方向-屏幕
- 十五.匿名函数
- Collections.sort()对HashMap里的键值对按照键进行排序
- Java多线程编程总结
- 安装 mysql - python
- JavaScript设置进度条
- byte[]数组工具类
- c++实现ping模块
- hadoop、大数据面试题(12)
- 浅析JNDI
- iOS-UIButton 全面解析
- win7系统开机无法进入系统怎么办?win7开机无法进入系统提示DHCP...的原因和解决方法
- IBatis.Net框架封装,快速开发
- 浅析java 的 static 关键字用法