(1)UIButton的创建
// UIButtonTypeCustom = 0, 自定义风格
// UIButtonTypeRoundedRect, 圆角矩形
// UIButtonTypeDetailDisclosure, 蓝色小箭头“>”按钮,主要做详细说明用
// UIButtonTypeInfoLight, 亮色感叹号
// UIButtonTypeInfoDark, 暗色感叹号
// UIButtonTypeContactAdd, 黑色“+”图标
UIButton*button = [UIButton buttonWithType:UIButtonTypeCustom];
(2)设置文字
//注意!下面这个是没有效果,查看UIButton的头文件就可以知道titleLabel这个属性是readonly
// button.titleLabel.text = @"自定义Button";
//设置内容
[button setTitle:@"自定义Button"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 = [UIFont systemFontOfSize:18];
// 设置内容文字的颜色
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
(6)设置Image,若设置image的同时设置title,则默认图片在左边文字在右边,文字放不下则默认文字中间以”…”替代
// 设置image
UIImage *photo = [UIImage imageNamed:@"image_photo"];
[button setImage:photo forState:UIControlStateNormal];
(7)设置BackgroundImage,这个是能够跟title共存的
//设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"image_photo"]forState:UIControlStateNormal];
(8)设置BackgroundColor:设置BackgroundImage会覆盖BackgroundColor
//设置Button背景颜色
button.backgroundColor = [UIColor redColor];
(9)设置内容距离边框的间距
// 设置内容距离边框
CGFloat contentPadding = 20;
button.contentEdgeInsets =UIEdgeInsetsMake(contentPadding, contentPadding, contentPadding, contentPadding);
(10)设置title距离边框的间距
// 设置title距离边框
CGFloat titlePadding = 10;
button.titleEdgeInsets =UIEdgeInsetsMake(titlePadding, titlePadding, titlePadding, titlePadding);
(11)设置image距离边框的间距
// 设置image距离
[button setImageEdgeInsets: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事件
// 设置监听事件
[button addTarget: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;
(16)设置文字超过button显示时的显示方式(换行符方式)
//6种方式
// typedef enum {
// UILineBreakModeWordWrap = 0, // Wrap at word boundaries
// UILineBreakModeCharacterWrap, // Wrap at character boundaries
// UILineBreakModeClip, // Simply clip when it hits the end of the rect
// UILineBreakModeHeadTruncation, // Truncate省略头部,使用..代替
// UILineBreakModeTailTruncation, // Truncate省略尾部,使用..代替
// UILineBreakModeMiddleTruncation, // Truncate 省略中间,使用..代替
// } UILineBreakMode;
转载自http://blog.csdn.net/djxiaoyu_haha/article/details/40341071