UIButton

来源:互联网 发布:程序员高清图 编辑:程序博客网 时间:2024/06/18 10:22

一、前言

    该博客里面的方法均是看着苹果官方的API来解释的,如有问题,请指出。

二、简介:

    UIButton就是一个按钮,它的应用十分广泛,比如登录,注册,忘记密码等功能的实现时都需要用到按钮。现在就让我们一起走进UIButton。

二、常用方法

    1、+ (id)buttonWithType:(UIButtonType)buttonType;  (IOS2及其以后有)

    该方法用与创建和返回一个带有制定样式的按钮。当使用该方法创建一个button,这个button的frame默认被设置为(0,0,0,0),所以当你要添加此方法创建的button到view时,首先应该更新一下frame。

    buttonType是一个枚举值。里面有几种不同的样式。下面是UIButtonType的枚举

复制代码
typedef NS_ENUM(NSInteger, UIButtonType) {    UIButtonTypeCustom = 0,                         // no button type    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // standard system button     UIButtonTypeDetailDisclosure,    UIButtonTypeInfoLight,    UIButtonTypeInfoDark,    UIButtonTypeContactAdd,
复制代码

 

    

    UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead

 

};

    使用该方法的例子:

  

复制代码
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];    button.frame =CGRectMake(10, 10, 230, 30);     [button setTitle:@"测试" forState:UIControlStateNormal];     [self.view addSubview:button];
复制代码

 

读者可以自行修改参数来设置按钮样式。

 

三、常用属性

    1、@property(nonatomic)UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR;(IOS2以及以后)

    UIEdgeInsets是一个结构体。具体实现如下:

复制代码
typedef struct UIEdgeInsets {    CGFloat top, left, bottom, right;  // specify amount to inset (positive) for each of the edges. values can be negative to 'outset' } UIEdgeInsets;
复制代码

 

 

该属性定义了视图的插入距离。 简单来说就是来设置button显示内容距离上、下、左、右的距离。

代码如下:  button.contentEdgeInsets = UIEdgeInsetsMake(100010);

 

读者可以自行查看。

    2、@property(nonatomic)UIEdgeInsets titleEdgeInsets;                // default is UIEdgeInsetsZero

        该属性类似与第一个属性,主要是设置button的title距离上、下、左、右的距离。上面的那个是设置内容的距离。

    3、@property(nonatomic)UIEdgeInsets imageEdgeInsets;                // default is UIEdgeInsetsZero

        该属性和上边两个类似,主要设置button上image的距离上下左右的距离。

    4、@property(nonatomicBOOL adjustsImageWhenHighlighted;    // default is YES. if YES, image is drawn darker when  highlighted(pressed)

        该属性默认是yes,它确定是否改变图片当button点击的时候。(没有测出来有什么变化!!!)

    5、@property(nonatomic,readonlyUIButtonType buttonType;

        该属性是只读的,用户获取button 类型,返回的枚举数字。0,1,2,3,4

    6、@property(nonatomic)BOOL  showsTouchWhenHighlighted;      // default is NO. if YES, show a simple feedback (currently a glow) while highlighted   

        该属性是控制当按钮点击的时候是否按钮本身发光。默认的是NO,不会发光,当设置为YES时,按钮将发高光

    7、@property(nonatomic)BOOL adjustsImageWhenDisabled;     // default is YES. if YES, image is drawn lighter when disabled

        该属性是决定当按钮不可点击时,图片是否改变。如果YES,那么图片被绘制的更加黑,前提是按钮不可点击。

    8、@property(nonatomic,readonly,retainNSString *currentTitle;  // normal/highlighted/selected/disabled. can return nil

        该属性用于得到当前button的title.

    9、@property(nonatomic,readonly,retainUIColor  *currentTitleColor; // normal/highlighted/selected/disabled. always returns non-nil. 

        该属性用于得到当前title的颜色

    10、都列出来吧

        @property(nonatomic,readonly,retainNSString *currentTitle;             // normal/highlighted/selected/disabled. can return nil

        @property(nonatomic,readonly,retainUIColor  *currentTitleColor;        // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)

        @property(nonatomic,readonly,retainUIColor  *currentTitleShadowColor;  // normal/highlighted/selected/disabled. default is white(0,0.5).

        @property(nonatomic,readonly,retainUIImage  *currentImage;             // normal/highlighted/selected/disabled. can return nil

        @property(nonatomic,readonly,retainUIImage  *currentBackgroundImage;   // normal/highlighted/selected/disabled. can return nil

        @property(nonatomic,readonly,retainNSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0);  // normal/highlighted/selected/

       11、

        // return title and image views. will always create them if necessary. always returns nil for system buttons

        @property(nonatomic,readonly,retainUILabel     *titleLabel NS_AVAILABLE_IOS(3_0);

        @property(nonatomic,readonly,retainUIImageView *imageView  NS_AVAILABLE_IOS(3_0);

        

        这两个属性都是只读的。titleLabel用户展示当前的title属性。imageView用户展示当前imageView;

        这些属性都是用与获取当前的属性值。和8、9一样。

        在重写按钮时,可以用这个两个属性去获取已经设置好的图片和title.

四、常用方法

    1、- (void)setTitle:(NSString *)title forState:(UIControlState)state;  // default is nil. title is assumed to be single line

        用来设置按钮显示的title。

        UIControlState是一个枚举类型。

复制代码
  typedef NS_OPTIONS(NSUInteger, UIControlState) {            UIControlStateNormal       = 0,            UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set            UIControlStateDisabled     = 1 << 1,            UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)            UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use            UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use        };
复制代码

 

  这个我亲测了一下,不过不同的state有时候我没有看出差别。(可能理解不深)

    2、- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR// default if nil. use opaque white

        该方法是用来设置显示title的颜色,在这里,UIControlState比较明显

        当设置为UIControlStateNormal,显示的是自己设置的颜色;

        当设置为UIControlStateHighlighted,也就是当按钮选中后的颜色为你设置的颜色

        当设置为UIControlStateDisabled,就是当按钮不可点击时,按钮title的颜色

        当设置为UIControlStateSelected,就是当按钮选择时,按钮的颜色。

        而后两个还不太清楚。而且用到的不多。

    3、- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR// default is nil. use 50% black

        设置title的阴影的颜色,后边的UIControlState和上面的是一样的,就不再赘述。

    4、- (void)setImage:(UIImage *)image forState:(UIControlState)state;       // default is nil. should be same size if different 

        这个方法用来设置按钮上左边的图片,UIControlState和上面的一样。

    5、- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR// default is nil

        该方法设置的是按钮的背景色。

    6、- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line

        设置按钮title,这个和第2个方法的区别是此方法会覆盖2方法,而且该方法可以设置带属性的字符串,然后作为title.

    7、以下的方法一起来说:

        - (NSString *)titleForState:(UIControlState)state;          // these getters only take a single state value

        - (UIColor *)titleColorForState:(UIControlState)state;

        - (UIColor *)titleShadowColorForState:(UIControlState)state;

        - (UIImage *)imageForState:(UIControlState)state;

        - (UIImage *)backgroundImageForState:(UIControlState)state;

 

        - (NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);

        它们都是getter方法,只读,用与获取不同UIControlState下的属性值,例如:

   

   [button setTitle:@"测ddd试" forState:UIControlStateDisabled];       NSString *s = [button titleForState:UIControlStateDisabled];

 

        s就是:测ddd试。其他类似。

    8、- (CGRect)titleRectForContentRect:(CGRect)contentRect;

       - (CGRect)imageRectForContentRect:(CGRect)contentRect;

    这两个放到一起说,它们很大程度上用与重写按钮时,在自己的按钮.m中可以自动调用这两个方法,这样就可以对这两个方法进行重写,使按钮title和image位置改变。实现自己需要的效果。

0 0