自定義按鈕UIControl
寫在前面
#圖示和文字在一起是開發難免的問題,系統的按鈕預設是圖片居左的文字居右的,
且圖片和文字的距離不好調整,圖片的位置更是讓人頭疼,
故在閒暇之餘封裝了一個控制元件。
複製程式碼
所用到知識的
# 1、蘋果系統自帶的自動佈局,減少第三方的依賴
# 2、kvo監聽UIControl的狀態
# 3、富文字的使用,如何更新約束,如何獲取一段文字的寬高
複製程式碼
先看看效果
擁有功能
1、任意調整圖片和文字的間距。
2、圖片的位置可以放置在上下左右不同位置。
3、支援富文字,自定義佈局,高亮和選中狀態。
複製程式碼
如何使用
/**
自定義按鈕初始化方法
@param image 預設的圖片
@param title 標題
@param titleFond 標題大小
@param imageMargin 標題與圖示的距離
@param imageAlignmentTYpe 圖片的顯示型別
@return 自定義按鈕的例項
*/
- (instancetype)initWithImage:(UIImage *) image
title:(NSString *) title
titleFond:(UIFont *) titleFond
imageMargin:(CGFloat) imageMargin
imageAlignmentTYpe:(MyButtonImageAlignmentTYpe )imageAlignmentTYpe;
_myButton = [[MyButton alloc]initWithImage:[UIImage imageNamed:@"cache_pause"]
title:@"來點我啊"
titleFond:[UIFont systemFontOfSize:14]
imageMargin:10
imageAlignmentTYpe:MyButtonImageAlignmentLeft];
[self.view addSubview:_myButton];
self.myButton.frame = CGRectMake(20, 100, 120, 40);
self.myButton.backgroundColor = UIColor.grayColor;
[self.myButton setImage:[UIImage imageNamed:@"cache_delete"] withState:UIControlStateSelected];
[self.myButton setImage:[UIImage imageNamed:@"cache_pause"] withState:UIControlStateHighlighted];
[self.myButton setTitle:@"選中了" withState:UIControlStateSelected];
[self.myButton setTitle:@"正在按下..." withState:UIControlStateHighlighted];
[self.myButton setTitleColor:UIColor.blueColor withState:UIControlStateSelected];
[self.myButton setTitleColor:UIColor.yellowColor withState:UIControlStateHighlighted];
這樣就可以了,和普通的按鈕用法一樣。
複製程式碼
關鍵程式碼
1、自定義佈局(只列出了一種)
//圖片居左
- (void)setImageLeftLayoutConstraint {
CGFloat imageWidth = self.normalImage.size.width;
CGFloat titleWidth = [self sizeWithText:self.titleLabel.text font:self.titleLabel.font].width;
CGFloat imageCenterXMargin = (imageWidth /2.0 - (imageWidth + titleWidth + self.imageMargin) / 2.0);
//建立Image約束
NSLayoutConstraint *imageCenterYLc = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
NSLayoutConstraint *imageCenterXLc = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:imageCenterXMargin];
[self addConstraints:@[imageCenterYLc,imageCenterXLc]];
//建立title約束
NSLayoutConstraint *titleCenterYLc = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
NSLayoutConstraint *titleLeftLc = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.imageView attribute:NSLayoutAttributeRight multiplier:1.0 constant:self.imageMargin];
[self addConstraints:@[titleCenterYLc,titleLeftLc]];
}
//獲取文字的寬高
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font {
NSDictionary *attrs = @{NSFontAttributeName : font};
return [text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
//kvo監聽
- (void)addObserver {
[self addObserver:self
forKeyPath:@"enabled"
options: NSKeyValueObservingOptionNew
context:nil];
[self addObserver:self
forKeyPath:@"selected"
options: NSKeyValueObservingOptionNew
context:nil];
[self addObserver:self
forKeyPath:@"highlighted"
options: NSKeyValueObservingOptionNew
context:nil];
[self.titleLabel addObserver:self
forKeyPath:@"text"
options:NSKeyValueObservingOptionNew
context:nil];
}
//kvo 監聽處理(只列出了部分)
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"selected"]) {
if(self.selected) {
self.imageView.image = self.selectedImage ? self.selectedImage : self.normalImage;
self.backgroundColor = self.selectedBackgroundColor ? self.selectedBackgroundColor : self.normalBackgroundColor;
if (!self.selectedTitleAttribute && !self.normalTitleAttribute) {
self.titleLabel.text = self.selectedTitle ? self.selectedTitle : self.normalTitle;
self.titleLabel.textColor = self.selectedTitleColor ? self.selectedTitleColor : self.normalTitleColor;
return;
}
self.titleLabel.attributedText = self.selectedTitleAttribute ? self.selectedTitleAttribute : self.normalTitleAttribute;
} else {
self.imageView.image = self.normalImage;
self.backgroundColor = self.normalBackgroundColor;
if (!self.normalTitleAttribute) {
self.titleLabel.text = self.normalTitle;
self.titleLabel.textColor = self.normalTitleColor;
return;
}
self.titleLabel.attributedText = self.normalTitleAttribute;
}
}
//監聽到字型變化,更新自動佈局
if ([keyPath isEqualToString:@"text"]) {
[self removeConstraints:self.constraints];
[self.superview layoutIfNeeded];
[self updateConstraints:self.imageAlignmentTYpe];
[self.superview layoutIfNeeded];
}
複製程式碼
以上只是部分程式碼,詳細程式碼請檢視
歡迎檢視MyApp
自定義按鈕 https://github.com/dudongge/MyApp
如果對您有幫助,請不吝star一下