iOS常用技术-九宫格button展示->自定义button
1 // 2 // CustomButton.m 3 // 九宫格 4 // 5 // Created by 大欢 on 16/1/21. 6 // Copyright © 2016年 bjsxt. All rights reserved. 7 // 8 9 #import "CustomButton.h" 10 11 static const CGFloat kScale = 0.7; 12 13 @implementation CustomButton 14 15 - (instancetype)initWithFrame:(CGRect)frame 16 { 17 self = [super initWithFrame:frame]; 18 if (self) { 19 20 self.imageView.contentMode = UIViewContentModeBottom; 21 self.titleLabel.textAlignment = NSTextAlignmentCenter; 22 self.titleLabel.font = [UIFont systemFontOfSize:20]; 23 24 } 25 return self; 26 } 27 28 - (CGRect)imageRectForContentRect:(CGRect)contentRect { 29 30 CGFloat ponitX = 0; 31 CGFloat ponitY = 0; 32 CGFloat width = contentRect.size.width; 33 CGFloat height = contentRect.size.height * kScale; 34 35 return CGRectMake(ponitX, ponitY, width, height); 36 } 37 38 - (CGRect)titleRectForContentRect:(CGRect)contentRect { 39 40 CGFloat ponitX = 0; 41 CGFloat ponitY = contentRect.size.height * kScale; 42 CGFloat width = contentRect.size.width; 43 CGFloat height = contentRect.size.height * (1 - kScale); 44 45 return CGRectMake(ponitX, ponitY, width, height); 46 } 47 48 @end
/***************************************************/
1 // 2 // ViewController.m 3 // 九宫格 4 // 5 // Created by 大欢 on 16/1/21. 6 // Copyright © 2016年 bjsxt. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "CustomButton.h" 11 12 @interface ViewController () 13 14 @end 15 16 @implementation ViewController 17 18 - (void)viewDidLoad { 19 [super viewDidLoad]; 20 21 NSArray * nameList = @[@"会议信息",@"会议日程",@"会议讲者",@"会议报道",@"论文摘要",@"会议讨论",@"照片墙",@"参会注册",@"地图",@"报名须知"]; 22 23 NSArray * imageList = @[@"meeting_info_n",@"meeting_schedule_n",@"meeting_guest_n",@"meeting_new_n",@"meeting_other_n",@"meeting_community_n",@"meeting_image_n",@"meeting_register_n",@"meeting_map_n",@"meeting_notice_n"]; 24 25 CGFloat width = CGRectGetWidth(self.view.frame)/3; 26 CGFloat height = 150; 27 28 for (int i = 0; i < nameList.count; i ++) { 29 30 CGFloat ponitX = i % 3; 31 CGFloat ponitY = i / 3; 32 33 CustomButton * btn = [CustomButton buttonWithType:UIButtonTypeCustom]; 34 btn.frame = CGRectMake(ponitX * width, ponitY * height, width, height); 35 [btn setTitle:nameList[i] forState:UIControlStateNormal]; 36 [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; 37 [btn setImage:[UIImage imageNamed:imageList[i]] forState:UIControlStateNormal]; 38 [btn addTarget:self action:@selector(logTitle:) forControlEvents:UIControlEventTouchUpInside]; 39 [self.view addSubview:btn]; 40 } 41 42 } 43 44 - (void)logTitle:(UIButton *)button { 45 46 NSLog(@"%@",button.currentTitle); 47 48 } 49 50 @end
/************************************************************/