ios - UIButton

2016-09-29  本文已影响34人  fjytqiu

创建:
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,                    //   蓝色的圆圈中加个“加号”
};

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];

btn.showsTouchWhenHighlighted = YES;

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

btn.tintColor = [UIColor greenColor];

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

    // 对应状态的文字
    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)backgroundRectForBounds:(CGRect)bounds;  
- (CGRect)contentRectForBounds:(CGRect)bounds; 
- (CGRect)titleRectForContentRect:(CGRect)contentRect; 
- (CGRect)imageRectForContentRect:(CGRect)contentRect; 

上一篇 下一篇

猜你喜欢

热点阅读