有時候,你會看到設(shè)計出來的界面某個位置可點擊,但是直接用按鈕又無法控制幾個元素的關(guān)系,這個時候與其用多個控件組合出來這樣的按鈕不如自己封裝一個來的快,還可以重復(fù)使用。雖然也需要計算元素的位置大小,但是多次使用的特性對于代碼的優(yōu)化起到至關(guān)重要的作用,看看博主要寫的按鈕長什么樣:

這樣一長條的可點擊區(qū)域,左右線條長短一致且垂直居中,中間為logo和對應(yīng)標(biāo)題,且多個按鈕的情況下按鈕長短一樣,線條根據(jù)內(nèi)容長短變化。
1.計算內(nèi)容長度; 2.確定logo尺寸或者相對尺寸; 3.按鈕長度減去1,2中的長度除以2就是線條長度; 4.為了美觀,減少線條長度,中間留白。
下面看代碼:
#import <UIKit/UIKit.h>@interface LHButton : UIButton- (void)setMyButtonIcon:(UIImage *)image titleTest:(NSString *)title titleFont:(CGFloat)fontSize titleColor:(UIColor *)color;@end#import "LHButton.h"@implementation LHButton/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code}*/- (void)setMyButtonIcon:(UIImage *)image titleTest:(NSString *)title titleFont:(CGFloat)fontSize titleColor:(UIColor *)color{ CGFloat titleWidth = [self configureLength:title titleFont:fontSize]; UIView *lineLeft = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height/2, (self.frame.size.width - titleWidth - 15 - 13)/2, 1)]; lineLeft.backgroundColor = [UIColor colorWithRed:0.84f green:0.83f blue:0.80f alpha:1.00f]; [self addSubview:lineLeft]; UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.frame.size.width - titleWidth - 15 - 13)/2 + 5, (self.frame.size.height - 13)/2, 13, 13)]; imageView.image = image; [self addSubview:imageView]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((self.frame.size.width - titleWidth - 15 - 13)/2 + 3 + 13 + 3, 0, titleWidth + 9, self.frame.size.height)]; label.text = title; label.textAlignment = NSTextAlignmentCenter; label.font = [UIFont systemFontOfSize:fontSize]; label.textColor = color; [self addSubview:label]; UIView *lineRight = [[UIView alloc]initWithFrame:CGRectMake(self.frame.size.width - (self.frame.size.width - titleWidth - 15 - 13)/2, self.frame.size.height/2, (self.frame.size.width - titleWidth - 15 - 13)/2, 1)]; lineRight.backgroundColor = [UIColor colorWithRed:0.84f green:0.83f blue:0.80f alpha:1.00f]; [self addSubview:lineRight];}#PRagma mark - 計算長度- (NSInteger)configureLength:(NSString *)titleTest titleFont:(CGFloat)fontSize{ NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]}; CGSize size = [titleTest boundingRectWithSize:CGSizeMake(1000, 0) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size; return size.width;}@end然后直接在VC中引用頭文件后使用:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. LHButton *myBtn = [LHButton buttonWithType:UIButtonTypeCustom]; myBtn.frame = CGRectMake(20, 100, 320 - 40, 40); [myBtn addTarget:self action:@selector(myBtnAction) forControlEvents:UIControlEventTouchUpInside]; [myBtn setMyButtonIcon:[UIImage imageNamed:@"icon-email.png"] titleTest:@"Sign up with Email" titleFont:13 titleColor:[UIColor colorWithRed:0.49f green:0.48f blue:0.46f alpha:1.00f]]; [self.view addSubview:myBtn];}- (void)myBtnAction{ NSLog(@"This is my Button");}如果你想要別的樣式的按鈕,換湯不換藥,都可以這么來搞。 Demo下載地址:點擊下載
新聞熱點
疑難解答
圖片精選