(1)UIButton的创建
// UIButtonTypeCustom = 0, 自定义风格
// UIButtonTypeRoundedRect, 圆角矩形
// UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
// UIButtonTypeInfoLight, 亮色感叹号
// UIButtonTypeInfoDark, 暗色感叹号
UIButton*button = [UIButtonbuttonWithType:UIButtonTypeCustom];
(2)设置文字
//注意这个是没有效果,查看UIButton的头文件就可以知道titleLabel这个属性是readonly
// button.titleLabel.text = @"自定义Button";
//设置内容
[buttonsetTitle:@"自定义Button"forState:UIControlStateNormal];
// 设置内容字体大小
button.titleLabel.font = [UIFontsystemFontOfSize:18];
// 设置内容文字的颜色
[buttonsetTitleColor:[UIColororangeColor]forState:UIControlStateNormal];
(3)状态
// UIControlStateNormal = 0, 常规状态显现
// UIControlStateHighlighted = 1 << 0, 高亮状态显现
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时
// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
(4)文字方向
// 水平方向
// UIControlContentHorizontalAlignmentCenter = 0,
// UIControlContentHorizontalAlignmentLeft = 1,
// UIControlContentHorizontalAlignmentRight = 2,
// UIControlContentHorizontalAlignmentFill = 3,
button.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
// 垂直方向
// UIControlContentVerticalAlignmentCenter = 0,
// UIControlContentVerticalAlignmentTop = 1,
// UIControlContentVerticalAlignmentBottom = 2,
// UIControlContentVerticalAlignmentFill = 3,
button.contentVerticalAlignment =UIControlContentVerticalAlignmentBottom;
(5)文字大小和颜色
// 设置内容字体大小
button.titleLabel.font = [UIFontsystemFontOfSize:18];
// 设置内容文字的颜色
[button setTitleColor:[UIColororangeColor]forState:UIControlStateNormal];
(6)设置Image,若设置image则设置title是无效的
// 设置image
UIImage *photo = [UIImageimageNamed:@"image_photo"];
[buttonsetImage:photoforState:UIControlStateNormal];
(7)设置BackgroundImage,这个是能够跟title共存的
//设置背景图片
[buttonsetBackgroundImage:[UIImageimageNamed:@"image_photo"]forState:UIControlStateNormal];
(8)设置BackgroundColor:设置BackgroundImage会覆盖BackgroundColor
//设置Button背景颜色
button.backgroundColor = [UIColorredColor];
(9)设置内容距离边框的间距
// 设置内容距离边框
CGFloatcontentPadding = 20;
button.contentEdgeInsets =UIEdgeInsetsMake(contentPadding, contentPadding, contentPadding, contentPadding);
(10)设置title距离边框的间距
// 设置title距离边框
CGFloat titlePadding =10;
button.titleEdgeInsets =UIEdgeInsetsMake(titlePadding, titlePadding, titlePadding, titlePadding);
(11)设置image距离边框的间距
// 设置image距离
[buttonsetImageEdgeInsets:UIEdgeInsetsMake(30, 30, 30, 30)];
(12)设置Button圆角,边框
// 设置Button圆角
[button.layersetMasksToBounds:YES];
[button.layersetCornerRadius:10.0];//设置矩形四个圆角半径
// 设置Button边框颜色和大小
[button.layersetBorderWidth:10];//边框宽度
[button.layersetBorderColor:[UIColorblueColor].CGColor];
(13)设置Button事件
// 设置监听事件
[buttonaddTarget:selfaction:@selector(onClick)forControlEvents:UIControlEventTouchUpInside];
(14)设置Button大小
CGSizeimageSize = button.currentImage.size;
button.frame =CGRectMake(10, 100, imageSize.width, imageSize.height);
根据文字设置大小
-(CGSize)siezOfText:(NSString *)text font:(UIFont *)font
{
//ios7复杂
NSDictionary *attrbute = @{NSFontAttributeName:font};
return [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attrbute context:nil].size;
//ios7简单
// return [text sizeWithAttributes:attrbute];
//ios6
//return [text sizeWithFont:font];
}
// 根据内容来设置Button宽度,高度
CGSize textSize = [self siezOfText:button.titleLabel.text font:button.titleLabel.font];
CGFloat w = textSize.width+间距;
CGFloat h = textSize.height+间距;
button.frame = CGRectMake(10, 100, w, h);
(15)设置Button其他
/*
*
* 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,那么可以去掉这个功能
*
*/
button.adjustsImageWhenHighlighted=NO;
/*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
button.adjustsImageWhenDisabled= NO;
/*下面的这个属性设置为yes的状态下,按钮按下会发光*/
button.showsTouchWhenHighlighted = YES;