iOS开发

UIButton

2019-04-08  本文已影响0人  追逐_chase
iOS.gif

常见的属性

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置frame
    button.frame = CGRectMake(0, 0, 100, 100);
    //设置字体
    [button setTitle:@"这是一个按钮" forState:UIControlStateNormal];
    //设置 高亮时的字体
     [button setTitle:@"这是一个按钮" forState:UIControlStateNormal];
      //设置字体大小
    button.titleLabel.font = [UIFont systemFontOfSize:18];
    //设置颜色
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    //设置图片
    [button setImage:[UIImage imageNamed:@"xx"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"xx"] forState:UIControlStateHighlighted];
    //背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"xx"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"xx"] forState:UIControlStateHighlighted];
   
    //设置圆角
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;


typedef NS_OPTIONS(NSUInteger, UIControlState) {
    UIControlStateNormal         //正常
    UIControlStateHighlighted    //高亮
    UIControlStateDisabled      //不可用
    UIControlStateSelected        //选中                  
    UIControlStateFocused           // 焦点 (不常用)
    UIControlStateReserved          // 
    };

//当前的的标题名称
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle;

//button的图片
@property(nullable, nonatomic,readonly,strong) UIImage  *currentImage;

//背景图片
@property(nullable, nonatomic,readonly,strong) UIImage  *currentBackgroundImage; 

//属性字符串
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle

UIButton的内部控件的更改

第一种方式

//更改label的位置
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    这里可以更改label的frame

 }
 
 //更改image的位置
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
   这里可以更改image的frame

  }

第二种方式

- (void)layoutSubviews{
    [super layoutSubviews];
    // 设置子控件的位置
    self.titleLabel.frame = CGRectMake(0, 0, 100, 70);
    self.imageView.frame = CGRectMake(100, 0, 70, 70);
}

UIButton的内边距

UIButton背景图片的拉伸

  // 1.1 创建按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // 1.2 设置frame
    button.frame = CGRectMake(50, 50, 300, 500);
    // 1.3 设置背景颜色
     button.backgroundColor = [UIColor purpleColor];
     UIImage *image = [UIImage imageNamed:@"liaotianbeijing"];
     CGFloat imageWidth = image.size.width;
     CGFloat imageHeight = image.size.height;
     NSLog(@"%f---%f",imageWidth,imageHeight);
    image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageHeight *0.7-1, imageWidth *0.3, imageHeight *0.3, imageWidth *0.7 -1)];
    // 1.4 设置背景图片
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [self.view addSubview:button];


// 右边需要保护的区域 = 图片的width - leftCapWidth - 1
     // bottom cap =  height - topCapHeight - 1
   UIImage *resizableImage = [image stretchableImageWithLeftCapWidth:imageWidth * 0.5 topCapHeight:imageHeight * 0.5];
//受保护的区域

裁剪.png
上一篇 下一篇

猜你喜欢

热点阅读