面对现实,超越自己
逆水行舟,不进则退
posts - 269,comments - 32,trackbacks - 0
创建两种方法:
(1)常规的initWithFrame的方式 
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 50, 100, 75)];

[btn1 setTitle:@"close" forState:UIControlStateNormal];
btn1.backgroundColor = [UIColor greenColor];//button的背景颜色
[btn1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];//button的背景图片

(2)UIButton 的一个类方法(也可以说是静态方法)buttonWithType 
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame = CGRectMake(200, 20, 50, 60);
btn2.backgroundColor = [UIColor blackColor];
[btn2 setTitle:@"clicke" forState:UIControlStateNormal];
[self.window addSubview:btn1];
[self.window addSubview:btn2];
 
能够定义的button类型由6种
typedef enum 

    UIButtonTypeCustom = 0,         // 自定义,无风格 
    UIButtonTypeRoundedRect,      // 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片 
    UIButtonTypeDetailDisclosure, // 蓝色的披露按钮,可放在任何文字旁 
    UIButtonTypeInfoLight,            // 微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁 
    UIButtonTypeInfoDark,            // 白色背景下使用的深色圆圈信息按钮 
    UIButtonTypeContactAdd,       // 蓝色加号(+)按钮,可以放在任何文字旁 
} UIButtonType; 
 
[btn1 setTitle:@"BTN1" forState:UIControlStateNormal];                                                                   //设置按钮的标题
 
[btn1 setTitle:@"高亮状态" forState:UIControlStateHighlighted];                                                        //高亮状态按钮title值

[btn2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];                             //你也可以为按钮的某一状态设置为图 

[btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];                                            //设置标题颜色 

[btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];                             //阴影 

[btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];  //背景图像 

btn1.titleLabel.font = [UIFont fontWithName:@“test” size:18];                                                       //设置按钮字体大小

[btn1 setTag:101] ;                                                                                                                            //设置tag值

btn1.layer.cornerRadius = 4.5;                                                                                                          //设置圆角——四个圆角半径 
btn1.layer.borderWidth = 0.5;                                                                                                          // 按钮边框宽度
                   
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();                                                   // 设置颜色空间为rgb,用于生成ColorRef
CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 1 });                        // 新建一个红色的ColorRef,用于设置边框(四个数字分别是 r, g, b, alpha)
btn1.layer.borderColor = borderColorRef;                                                                                        // 设置边框颜色

更多属性参考链接:http://www.cocoachina.com/wiki/index.php?title=UIButton&variant=zh-cn

参数 forState . 这个参数决定了标题、图像或其他属性将在何种状态下显现。你可以编程令按钮在那个状态变化 
enum { 
    UIControlStateNormal       = 0,                    // 常态                      
    UIControlStateHighlighted  = 1 << 0,          // 高亮 
    UIControlStateDisabled     = 1 << 1,           // 禁用 
    UIControlStateSelected     = 1 << 2,           // 选中 
    UIControlStateApplication  = 0x00FF0000,  // 当应用程序标志使用时 
    UIControlStateReserved     = 0xFF000000  // 为内部框架预留的 
}; 
typedef NSUInteger UIControlState; 
 
你只要掌握前四种状态就好了。
当按钮高亮或者禁用,UIButton 类可以调整自己的外观,下面几个属性可以让你按照需要对按钮的外观进行微调:
adjustsImageWhenHighlighted
默认情况下,在按钮被禁用时,图像会被画的颜色深一些。要禁用此功能,请将这个属性设置为NO:
btn1.adjustsImageWhenHighlighted = NO; 

adjustsImageWhenDisabled
默认情况下,按钮在被禁用时,图像会被画的颜色淡一些。要禁用此功能,请将这个属性设置为NO:
btn1.adjustsImageWhenDisabled = NO; 

showsTouchWhenHighlighted
这个属性设置为YES,可令按钮在按下时发光。这可以用于信息按钮或者有些重要的按钮:
btn1.showsTouchWhenHighlighted = YES;
 
显示控件
[self.view addSubview:btn1]; 
[self.view addSubview:btn2]; 
 
重写绘制行为
 
你可以通过子类化按钮来定制属于你自己的按钮类。在子类化的时候你可以重载下面这些方法,这些方法返回CGRect结构,指明了按钮每一组成部分的边界。
注意:不要直接调用这些方法, 这些方法是你写给系统调用的。
 
backgroundRectForBounds   //指定背景边界 
contentRectForBounds         // 指定内容边界 
titleRectForContentRect       // 指定文字标题边界  
imageRectForContentRect   //指定按钮图像边界 
 
例:
- (CGRect)imageRectForContentRect:(CGRect)bounds

     return CGRectMake(0.0, 0.0, 44, 44); 

 
[btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];//添加点击按钮事件
 
-(void)btnPressed:(id)sender
{  
      UIButton* btn = (UIButton*)sender;  
      //开始写你自己的动作 
}

forControlEvents参数类型
 typedef NS_OPTIONS(NSUInteger, UIControlEvents) 
{
    UIControlEventTouchDown                 = 1 <<  0,      // 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
    UIControlEventTouchDownRepeat      = 1 <<  1,      // 多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
    UIControlEventTouchDragInside         = 1 <<  2,      // 当一次触摸在控件窗口内拖动时。
    UIControlEventTouchDragOutside       = 1 <<  3,      // 当一次触摸在控件窗口之外拖动时。
    UIControlEventTouchDragEnter           = 1 <<  4,      // 当一次触摸从控件窗口之外拖动到内部时
    UIControlEventTouchDragExit             = 1 <<  5,      // 当一次触摸从控件窗口内部拖动到外部时。
    UIControlEventTouchUpInside            = 1 <<  6,      // 所有在控件之内触摸抬起事件
    UIControlEventTouchUpOutside          = 1 <<  7,      // 所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
    UIControlEventTouchCancel                = 1 <<  8,      //所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。

    UIControlEventValueChanged             = 1 << 12,     // 当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。

    UIControlEventEditingDidBegin           = 1 << 16,     // 当文本控件中开始编辑时发送通知
    UIControlEventEditingChanged           = 1 << 17,     // 当文本控件中的文本被改变时发送通知。
    UIControlEventEditingDidEnd              = 1 << 18,     // 当文本控件中编辑结束时发送通知。
    UIControlEventEditingDidEndOnExit    = 1 << 19,     // 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。

    UIControlEventAllTouchEvents             = 0x00000FFF,  // 通知所有触摸事件。
    UIControlEventAllEditingEvents           = 0x000F0000,  // 通知所有关于文本编辑的事件。
    UIControlEventApplicationReserved    = 0x0F000000,  // range available for application use
    UIControlEventSystemReserved          = 0xF0000000,  // range reserved for internal framework use
    UIControlEventAllEvents                      = 0xFFFFFFFF   // 通知所有事件
}; 
posted on 2014-07-15 13:37 王海光 阅读(6249) 评论(0)  编辑 收藏 引用 所属分类: IOS

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理