美文网首页
ios - UIButton

ios - UIButton

作者: fjytqiu | 来源:发表于2016-09-29 10:04 被阅读34次

创建:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(100, 100, 100, 100);

typedef NS_ENUM(NSInteger, UIButtonType) {
    UIButtonTypeCustom = 0,                         //  自定义
    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  //  系统默认风格 
   // 以下三种 创建出来的按钮一样,蓝色的圆圈中加个“叹号”
    UIButtonTypeDetailDisclosure,
    UIButtonTypeInfoLight,
    UIButtonTypeInfoDark,

    UIButtonTypeContactAdd,                    //   蓝色的圆圈中加个“加号”
};
  • 设置内容方法

    1.设置文字内容

    [btn setTitle:@"我是按钮" forState:UIControlStateNormal];

    2.设置文字颜色

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

    3.设置字体

    [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];

4.设置图片

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

5.设置背景图片

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

6.设置带属性文字

[btn setAttributedTitle:[[NSAttributedString alloc] initWithString:@"带属性文字" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]}] forState:UIControlStateNormal];

7.设置阴影文字颜色

[btn setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];

  • 属性

    1.设置整体内容(文字+图片)偏移量

    btn.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 15, 20);

    2.设置文字内容偏移量

    btn.titleEdgeInsets = UIEdgeInsetsMake(5, 10, 15, 20);

    3.设置图片内容偏移量

    btn.imageEdgeInsets = UIEdgeInsetsMake(5, 10, 15, 20);

    4.高亮状态时图片颜色变暗(默认NO)

    btn.adjustsImageWhenHighlighted = YES;

    5.禁用状态时图片颜色变暗(默认NO)

    btn.adjustsImageWhenDisabled = YES;

    6.显示点击高亮时的位置(默认NO)

btn.showsTouchWhenHighlighted = YES;

7.对标题和图片作用,并且只作用于系统style

btn.tintColor = [UIColor greenColor];

8.按钮高亮时,改变阴影效果 (默认NO)

  • 获取信息的get 方法和只读属性

    // 对应状态的文字
    NSString *title = [btn titleForState:UIControlStateNormal];
    // 对应状态的文字颜色   
    UIColor *titleColor = [btn titleColorForState:UIControlStateNormal];
    // 对应状态的阴影文字颜色  
    UIColor *shadowColor = [btn titleShadowColorForState:UIControlStateNormal];  
    // 对应状态的图片
    UIImage *image = [btn imageForState:UIControlStateNormal];
    // 对应状态的背景图片
    UIImage *bgImage = [btn backgroundImageForState:UIControlStateNormal];
    // 对应状态的属性文字
    NSAttributedString *attrStr = [btn attributedTitleForState:UIControlStateNormal];
    
    // 当前状态的文字
    NSString *title1 = btn.currentTitle;
    // 当前状态的文字颜色
    UIColor *titleColor1 = btn.currentTitleColor;
    // 当前状态的阴影文字
    UIColor *shadowColor1 = btn.currentTitleShadowColor;
    // 当前状态的图片
    UIImage *image1 = btn.currentImage;
    // 当前状态的背景图片
    UIImage *bgImage1 = btn.currentBackgroundImage;
    // 当前状态的属性文字
    NSAttributedString *attrStr1 = btn.currentAttributedTitle;
  • 方法

    一般用于在子类化按钮的时候重载下面这些方法, 返回CGRect,指明按钮每组成部分的边界,达到定制属于自己的按钮类。
    注意:不要直接调用这些方法, 这些方法是你写给系统调用的。
- (CGRect)backgroundRectForBounds:(CGRect)bounds;  
- (CGRect)contentRectForBounds:(CGRect)bounds; 
- (CGRect)titleRectForContentRect:(CGRect)contentRect; 
- (CGRect)imageRectForContentRect:(CGRect)contentRect; 

相关文章

网友评论

      本文标题:ios - UIButton

      本文链接:https://www.haomeiwen.com/subject/xnlkyttx.html