/**
* 添加九宫格按钮
*/
-(void)setupBtnWithBtnArr:(NSMutableArray *)arr {
//
int totalloc =3;
CGFloat btnVW = (mDeviceWidth-20*4)/3;
CGFloat btnVH = 30;
CGFloat margin =15;
int count =(int)arr.count;
for (int i=0;i<count;i++){
int row =i/totalloc; //行号
int loc =i%totalloc; //列号
CGFloat btnVX =margin+(margin +btnVW)*loc;
CGFloat btnVY =margin+(margin +btnVH)*row;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(btnVX, btnVY, btnVW, btnVH);
// [btn setTitle:[NSString stringWithFormat:@"%@",arr[i]] forState:UIControlStateNormal];
[btn setTitle:[NSString stringWithFormat:@"%@",[arr[i] objectForKey:@"typeName"]] forState:UIControlStateNormal];
[btn setTitleColor:fontHightRedColor forState:UIControlStateSelected];
[btn setTitleColor:fontNomalColor forState:UIControlStateNormal];
btn.titleLabel.font =[UIFont fontWithName:uFont size:10];
self.cellBtn=btn;
btn.tag=i+1000;
btn.layer.cornerRadius=5;
btn.layer.borderColor =[UIColor colorWithWhite:0.6 alpha:1].CGColor;
btn.layer.borderWidth=0.6;
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview: btn];
}
}
//单项点击按钮事件
-(void)btnClick:(UIButton *)cender{
for (int i=0;i<self.cellBtnArr.count;i++){
UIButton *btn = [self viewWithTag:i+1000];
if(btn.tag!=cender.tag){
btn.layer.borderColor =[UIColor colorWithWhite:0.6 alpha:1].CGColor;
btn.layer.borderWidth=0.6;
btn.selected=NO;
}else {
btn.layer.borderColor =fontHightRedColor.CGColor;
btn.layer.borderWidth=0.6;
btn.selected=YES;
if (self.cellSelectBtnBlock) {
self.cellSelectBtnBlock([self.cellBtnArr[i] objectForKey:@"id"]);
}
DLog(@"btn:tag%ld",btn.tag);
}
}
}
//布局不规则的按钮样式
-(void)addTypeBtnView{
UIView *btnView =[[UIView alloc]init];
btnView.frame =CGRectMake(0, 110, mDeviceWidth, 60);
btnView.backgroundColor=[UIColor whiteColor];
[self.view addSubview:btnView];
NSArray *arr =[[NSArray alloc]initWithObjects:@"精选住宿",@"大气舒适",@"配套齐全",@"美味",@"漂亮",@"干净",@"哈哈哈哈哈",@"精选住",@"舒适",@"配套齐全齐全",nil];
CGFloat width =30;
CGFloat j=0;
CGFloat row =1;
for (int i =0;i<arr.count;i++){
CGFloat btnWidth = [KNHelper longFloatWithstring:arr[i] andHeight:30 andSize:12]+30;
//
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10*j+width,row*35,btnWidth, 30);
[btn setTitle:arr[i] forState:UIControlStateNormal];
btn.titleLabel.font=[UIFont fontWithName:uFont size:12];
btn.layer.borderWidth=1;
btn.layer.borderColor=borderCol.CGColor;
btn.layer.cornerRadius=3;
[btn setTitleColor:fontHightColor forState:UIControlStateNormal];
[btn setTitleColor:fontHightRedColor forState:UIControlStateSelected];
[btn addTarget:self action:@selector(btnClicke:) forControlEvents:UIControlEventTouchUpInside];
btn.tag=i+100;
[btnView addSubview: btn];
width=width+btnWidth;
j++;
if (width>mDeviceWidth-60) {
j=0;
width=30;
row++;
btn.frame = CGRectMake(10*j+width, row * 35,btnWidth, 30);
width =width+btnWidth;
j++;
DLog(@"row:%f",row);
}
typeBtnHight =(row+2)*35;
DLog(@"row33333:%f",row);
btnView.frame =CGRectMake(0, 110, mDeviceWidth,typeBtnHight);
}
}
//点击多选按钮事件
-(void)btnClicke:(UIButton *)sender{
UIButton *btn =[self.view viewWithTag:sender.tag];
if(btn.selected==YES){
btn.selected=NO;
btn.layer.borderColor=borderCol.CGColor;
} else {
btn.selected=YES;
btn.layer.borderColor=fontHightRedColor.CGColor;
}
}
<pre name="code" class="objc">//判断按钮title长度
+(float)longFloatWithstring:(NSString *)aString
andHeight:(float)aHeigt
andSize:(float)aSize
{
CGSize textSiz = [aString sizeWithFont:[UIFont fontWithName:uFont size:aSize]
constrainedToSize:CGSizeMake(MAXFLOAT, aHeigt)];
float textHeight = textSiz.width;
return textHeight;
}
//类似淘宝评论的星星按钮布局
-(void)addStarBtnView{
for (int i =0;i<5;i++){
//
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake((mDeviceWidth-200)/2+i*40+5, 80, 30, 30);
[btn setBackgroundImage:[UIImage imageNamed:@"star_nomal"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"star_helight"] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(starClick:) forControlEvents:UIControlEventTouchUpInside];
btn.tag=i+1000;
[self.view addSubview: btn];
}
}
-(void)starClick:(UIButton *)sender{
for(int i=0;i<5 ;i++){
UIButton *btn =[self.view viewWithTag:i+1000];
if(btn.tag <=sender.tag){
btn.selected=YES;
}else{
btn.selected=NO;
}
}
}