UIButton用法大全

#import “AppDelegate.h”

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    /* 创建UIButton对象 */

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.window addSubview:button];
    button.frame = CGRectMake(100, 100, 100, 130);

    /* UIButtonType 类型 */

    /*
    typedef enum {
    UIButtonTypeCustom = 0, 自定义模式
    UIButtonTypeSystem, 系统模式
    UIButtonTypeDetailDisclosure, 详情信息模式
    UIButtonTypeInfoLight, 带有亮色背景的信息模式按键
    UIButtonTypeInfoDark, 带有暗色背景的信息模式按键
    UIButtonTypeContactAdd, 联系人增加模式
    } UIButtonType;
    */

pragma mark - 设置属性

// titleLabel 标题文本框 只读属性

/* 设置标题 */

[button setTitle:@"WELCOME" forState:UIControlStateNormal];

/* forstate状态: 这个参数决定了标题, 图像或其他属性将在何种状态下显现 */

/*
 enum {
 UIControlStateNormal            = 0,            常态
 UIControlStateHighlighted       = 1 << 0,       is set 高亮
 UIControlStateDisabled          = 1 << 1,       禁用
 UIControlStateSelected          = 1 << 2,       选中
 UIControlStateApplication       = 0x00FF0000,   当应用程序标志使用时
 UIControlStateReserved          = 0xFF000000    为内部框架预留的
 };
 */

/* 标题状态 */

[button titleForState:UIControlStateNormal];

/* 设置属性标题, 如果你设置了两种标题模式, 按钮会使用属性标题 */

[button setAttributedTitle:[[NSAttributedString alloc] initWithString:@""] forState:UIControlStateNormal];

/* 设置属性标题状态 */

[button attributedTitleForState:UIControlStateNormal];

/* 设置标题颜色 */

[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

/* 设置标题颜色状态 */

[button titleColorForState:UIControlStateNormal];

/* 设置标题阴影颜色 */

[button setTitleShadowColor:[UIColor greenColor] forState:UIControlStateNormal];

/* 设置标题阴影颜色状态 */

[button titleShadowColorForState:UIControlStateNormal];

/* 当高亮时是否能够预留标题阴影, 默认值为NO */

button.reversesTitleShadowWhenHighlighted = YES;

pragma mark - 按钮外观调整

/* 按钮在高亮状态下, 图像会更亮, 默认为YES */

button.adjustsImageWhenHighlighted = YES;

/* 按钮在禁用状态下, 图像会更暗, 默认为YES */

button.adjustsImageWhenDisabled = YES;

/* 是按钮在按下时发光, 默认为YES */

button.showsTouchWhenHighlighted = YES;

/* 设置背景图片 */

[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];

/* 设置背景图片状态 */

[button backgroundImageForState:UIControlStateNormal];

/* 设置图片, 在按钮上边设置图片 */

[button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];

/* 设置图片状态 */

[button imageForState:UIControlStateNormal];

/* 设置色彩, 按钮模式需要改为UIButtonTypeCustom */

button.tintColor = [UIColor greenColor];

pragma mark - 边缘插页

/* 设置内容边缘插页, 默认为UIEdgeInsetsZero */

//button.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);

/* 设置标题边缘插页, 默认为UIEdgeInsetsZero */

//button.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);

/* 设置图片边缘插页, 默认为UIEdgeInsetsZero */

//button.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);

pragma mark - 获取按钮状态

/* 获取按钮类型 */

NSLog(@"%ld", button.buttonType);

/* 获取按钮标题 */

NSLog(@"%@", button.currentTitle);

/* 获取按钮属性标题 */

NSLog(@"%@", button.currentAttributedTitle);

/* 获取按钮标题颜色 */

NSLog(@"%@", button.currentTitleColor);

/* 获取按钮标题阴影颜色 */

NSLog(@"%@", button.currentTitleShadowColor);

/* 获取按钮图片 */

NSLog(@"%@", button.currentImage);

/* 获取按钮背景图片 */

NSLog(@"%@", button.currentBackgroundImage);

/* 获取图片视图 */

NSLog(@"%@", button.imageView);

pragma mark - 重写绘制行为

//- backgroundImageForState: 指定背景边界

//- contentRectForBounds: 指定内容边界

//- titleRectForContentRect: 指定文字标题边界

//- imageRectForContentRect: 指定按钮图像边界

pragma mark - 添加动作

[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

/* UIControlEvents */
/*
enum {
    单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
    UIControlEventTouchDown           = 1 <<  0,

    多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
    UIControlEventTouchDownRepeat     = 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,

    通知所有触摸事件
    UIControlEventAllTouchEvents      = 0x00000FFF,

    为应用使用, 使一系列控制事件有效
    UIControlEventApplicationReserved = 0x0F000000,

    为内置框架使用, 使一系列控制事件有效
    UIControlEventSystemReserved      = 0xF0000000,

    通知所有事件。
    UIControlEventAllEvents           = 0xFFFFFFFF


    //UITextField 的点击事件

    当文本控件中开始编辑时发送通知。
    UIControlEventEditingDidBegin     = 1 << 16,

    当文本控件中的文本被改变时发送通知。
    UIControlEventEditingChanged      = 1 << 17,

    当文本控件中编辑结束时发送通知
    UIControlEventEditingDidEnd       = 1 << 18,

    当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知
    UIControlEventEditingDidEndOnExit = 1 << 19,

    通知所有触摸事件
    UIControlEventAllEditingEvents    = 0x000F0000,

};
*/



[_window release];
return YES;

}

  • (void)buttonClicked:(UIButton *)button
    {

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值