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的内部控件的更改
- 在button内部有2个控件 UIlabel和UIImageView
- 更改这2个控件的位置,需要自定义button,实现下面的方法
第一种方式
//更改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的内边距
-
contentEdgeInsets
按钮的内边距 -
imageEdgeInsets
按钮图片的内边距 -
titleEdgeInsets
按钮图片的内边距
UIButton背景图片的拉伸
- UIImage中有3种关于图片剪切的方法,一般是剪切取中间1像素拉伸
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets ;
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
- 其中方法2中 枚举有2个
UIImageResizingModeTile, 平铺
,UIImageResizingModeStretch, 拉伸(伸缩)
// 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];
- 第3中裁剪
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
// 右边需要保护的区域 = 图片的width - leftCapWidth - 1
// bottom cap = height - topCapHeight - 1
UIImage *resizableImage = [image stretchableImageWithLeftCapWidth:imageWidth * 0.5 topCapHeight:imageHeight * 0.5];
//受保护的区域
裁剪.png