iOS基础-UIButton属性详解

/** 初始化buttonWithType类型

UIButtonTypeCustom = 0,默认无样式

UIButtonTypeSystem,系统按钮(iOS 7以上版本),慎用

UIButtonTypeDetailDisclosure,蓝色箭头按钮

UIButtonTypeInfoLight,亮色圆圈叹号按钮

UIButtonTypeInfoDark,深色圆圈叹号按钮

UIButtonTypeContactAdd,蓝色加号按钮

UIButtonTypeRoundedRect圆角矩形

*/

TTButton *button = [TTButtonbuttonWithType:UIButtonTypeCustom];

//按钮尺寸

button.frame = (CGRect){CGPointZero,CGSizeMake(100,100)};

//按钮位置

button.centerself.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);

/* 设置titletitleColorimagebackgroundImage等属性的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文本不显示问题,打印出来看一下(<UIButtonLabel: 0x7f9070fc46b0; frame = (0 0; 0 0); text = 'Test'; hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7f9070fc4d50>>)发现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.viewaddSubview:button];


/**重写按钮的方法,有些常有的按钮需求,按给出的属性设置会比较棘手,这时就会用到下面几个方法

下面演示titleRectForContentRectimageRectForContentRect两个方法

- (CGRect)backgroundRectForBounds:(CGRect)bounds;

- (CGRect)contentRectForBounds:(CGRect)bounds;

- (CGRect)titleRectForContentRect:(CGRect)contentRect;常用

- (CGRect)imageRectForContentRect:(CGRect)contentRect;常用

*/

//首先新建一个类,继承于UIButton

// 在.m文件重写titleRectForContentRectimageRectForContentRect两个方法

@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:如有错误及遗漏,欢迎指点!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值