署名: 那年高中今日还
IOS button
1.创建
+ buttonWithType: // 创建并返回一个特定风格的按钮
2.button上的文字
– setTitle:forState:
– setTitleColor:forState:
– titleColorForState: // 返回不同状态时标题颜色.
– titleForState: // 返回不同状态时的标题
//获取button的title
currentTitle property // 当前按钮上显示的标题(只读),当按钮状态改变时值自动改变.值可以为nil.
currentTitleColor property // 当前标题颜色(只读).此值要保证不为nil,默认是白色.
3. button 上的图片
adjustsImageWhenHighlighted property // 确定当按钮高亮时图片是否改变的BOOL值,为真时图片随按钮高亮而高亮
adjustsImageWhenDisabled property // 确定当按钮高亮时图片是否改变的BOOL值,为真时图片随按钮失效而变暗
showsTouchWhenHighlighted property // 控制当按钮按下时是否闪光的BOOL值.默认NO,YES时按下会有白色光点.图片和按钮事件的不会因闪光改变.
– backgroundImageForState: // 返回某个按钮状态下使用的背景图片.
– imageForState: // 返回某个状态下的按钮图片.
– setBackgroundImage:forState: // 设置特定状态的背景图片,默认都是normal
– setImage:forState: // 设置特定状态的图片,默认都是normal
4.设置button内部子控件的layout
contentEdgeInsets
titleEdgeInsets
imageEdgeInsets
5. rect
– backgroundRectForBounds: // 返回背景绘制区域.
– contentRectForBounds: // 返回内容绘制区域.内容区域是显示图片和标题及他们特定对齐缩放等的范围.
– titleRectForContentRect: // 返回标题的绘制区域.
– imageRectForContentRect: // 返回图片的绘制区域.
button的基本使用
_button = [UIButtonbuttonWithType:0];
_button.frame = CGRectMake(0, 0, 200, 200);
_button.center = self.view.center;
[_buttonsetBackgroundColor:[UIColorblueColor]];
[_buttonsetImage:[UIImageimageNamed:@"1111.jpeg"] forState:UIControlStateNormal];
[_buttonsetTitle:@"测试专用"forState:UIControlStateNormal];
[self.viewaddSubview:_button];
[_buttonsetExclusiveTouch:YES]; //禁止多个button同时点击
//设置圆角
[_button.layersetMasksToBounds:YES];
[_button.layersetCornerRadius:_button.bounds.size.width/2];
[_button addTarget:selfaction:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[selfajustTitleAndImageLocation];
// Do any additional setup after loadingthe view, typically from a nib.
}
- (void) ajustTitleAndImageLocation
{
// UIEdgeInsets 属性
// UIEdgeInsets
// {
// CGFloat top, left, bottom, right //逆时钟
// }UIEdgeInsets
// 创建UIEfgeInsets方法 UIEdgeInsetsMake 所有结构体创建方法都是这样
CGSize size = _button.bounds.size;
CGSize size_image = [_button imageView].bounds.size;
CGSize size_title = [_button titleLabel].bounds.size;
//关于button中的这三个属性 ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
// [_buttonsetContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
_button.imageEdgeInsets = UIEdgeInsetsMake(0,(size.width-size_image.width)/2,100,0);
_button.titleEdgeInsets = UIEdgeInsetsMake(size_image.height,-size_image.width-28, 0, 0);
}
- (void) buttonClick:(UIButton*) sender
{
NSLog(@"%@", [sender currentTitle]);
}