iOS九宫格布局按钮样式/不规则布局按钮样式/类似淘宝星星布局按钮样式

/**
 *  添加九宫格按钮
 */
-(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;
        }
    }
    
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值