/** 初始化buttonWithType类型
UIButtonTypeCustom = 0,默认无样式
UIButtonTypeSystem,系统按钮(iOS 7以上版本),慎用
UIButtonTypeDetailDisclosure,蓝色箭头按钮
UIButtonTypeInfoLight,亮色圆圈叹号按钮
UIButtonTypeInfoDark,深色圆圈叹号按钮
UIButtonTypeContactAdd,蓝色加号按钮
UIButtonTypeRoundedRect圆角矩形
*/
TTButton *button = [TTButtonbuttonWithType:UIButtonTypeCustom];
//按钮尺寸
button.frame = (CGRect){CGPointZero,CGSizeMake(100,100)};
//按钮位置
button.center =self.view.center;
//按钮背景色
[button setBackgroundColor:[UIColor orangeColor]];
// tintColor属性慎用,这跟上面所说的UIButtonTypeSystem类型有关系,如果初始化类型为UIButtonTypeSystem,同样也设置了tintColor属性,则在设置图片的时候会收到这个tintColor颜色的影响
[button setTintColor:[UIColoryellowColor]];
//默认为NO,如果设置为YES,当按钮的状态进行切换时,(如果设置了图片则图片,否则就是文本)会闪出类似位灯的闪烁效果
button.reversesTitleShadowWhenHighlighted =NO;
//默认为YES,当按钮高亮的时候,按钮图片会变暗,设置NO为取消此功能
button.adjustsImageWhenHighlighted =NO;
//与上面属性一样,此为禁用状态
button.adjustsImageWhenDisabled =NO;
//属性设置为YES的状态下,按钮按下会发光
button.showsTouchWhenHighlighted =YES;
// UIEdgeInsetsMake(上,左,底,右)内容(图片与文本)间距
button.contentEdgeInsets =UIEdgeInsetsMake(0,0, 0,0);
//文本间距
button.titleEdgeInsets =UIEdgeInsetsMake(0,0, 0,0);
//图片间距
button.imageEdgeInsets =UIEdgeInsetsMake(0,0, 0,0);
/* 设置title、titleColor、image、backgroundImage等属性的forState状态
UIControlStateNormal普通状态
UIControlStateHighlighted高亮状态
UIControlStateDisabled禁用状态
UIControlStateSelected选中状态
UIControlStateFocused聚焦状态(iOS 9新增)
UIControlStateApplication当应用程序标志使用时
UIControlStateReserved内部框架预留使用,不用管
*/
[button setTitleColor:[UIColorwhiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColorblackColor] forState:UIControlStateSelected];
[button setTitle:@"按钮"forState:UIControlStateNormal];
[button setTitle:@"选中"forState:UIControlStateSelected];
// titleLabel属性是只读的,但是他的属性是可读写。另外关于titleLabel文本不显示问题,打印出来看一下(>)发现frame = (0 0; 0 0),hidden = YES。但是设置这两个属性后依然没有显示,hidden依旧为YES。目前还不明白这是为何
button.titleLabel.text =@"Test";
//按钮图片,不会覆盖整个图片
[button setImage:[UIImageimageNamed:@"image"]forState:UIControlStateNormal];
[button setImage:[UIImageimageNamed:@"imageSelected"]forState:UIControlStateSelected];
//按钮背景图片,会覆盖整个按钮
[button setBackgroundImage:[UIImageimageNamed:@"bgImage"]forState:UIControlStateNormal];
[button setBackgroundImage:[UIImageimageNamed:@"bgImage"]forState:UIControlStateNormal];
/**监听按钮的选中事件,方法clickButton必须实现,不然会crash
UIControlEventTouchDown按下触发
UIControlEventTouchDownRepeat按下多次触发
UIControlEventTouchDragInside按下后在按钮一定的外围拖动触发
UIControlEventTouchDragOutside保持按下,在按钮外面拖动
UIControlEventTouchDragEnter DragOutside->DragInside触发
UIControlEventTouchDragExit内到外触发
UIControlEventTouchUpInside在按钮一定外围内松开触发
UIControlEventTouchUpOutside按钮外面松开
UIControlEventTouchCancel点击取消
*/
[button addTarget:selfaction:@selector(clickButton:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
/**重写按钮的方法,有些常有的按钮需求,按给出的属性设置会比较棘手,这时就会用到下面几个方法
下面演示titleRectForContentRect和imageRectForContentRect两个方法
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
- (CGRect)contentRectForBounds:(CGRect)bounds;
- (CGRect)titleRectForContentRect:(CGRect)contentRect;常用
- (CGRect)imageRectForContentRect:(CGRect)contentRect;常用
*/
//首先新建一个类,继承于UIButton
// 在.m文件重写titleRectForContentRect和imageRectForContentRect两个方法
@implementation TTButton
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGRect frame = contentRect;
frame.origin.x =10;
frame.origin.y =10;
frame.size.width =50;
frame.size.height =50;
return frame;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
//可根据文本的宽度设置图片的位置,根据字体获取宽度可看我上篇UILabel文章
NSString *string =self.currentTitle; // 获取当前文本
CGRect frame = contentRect;
frame.origin.x =10;
frame.origin.y =10;
frame.size.width =50;
frame.size.height =50;
return frame;
}
@end
PS:适用初学者,希望能帮助到需要的人,如有不对,欢迎指点!
网友评论