博客> Button的图片和文字间距调整(复选按钮)
Button的图片和文字间距调整(复选按钮)
2小时前 评论:0 阅读:254 飞翔信念
Button的图片和文字间距调整(复选按钮)

list text here今天要实现一个按钮复选功能,并且自带图片和文字主要参考了一下这篇帖子http://www.cnblogs.com/xilifeng/p/4827808.html 附上代码及图片:  Enter your image description here:

for (int i=0; i<array.count; i++) {
                    UIButton * button=[UIButton buttonWithType:UIButtonTypeCustom];
                    button.frame=CGRectMake((DeviceWidth-240)/2+150*(i%2), 96+40*(i/2), 90,23);
                    button.titleLabel.font=[UIFont systemFontOfSize:15];
                    [button setTitle:array[i][@"reason"] forState:UIControlStateNormal];
                    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
                    [button setTitleColor:NAVE_BACKCOLOR forState:UIControlStateSelected];
                    [button setImage:[UIImage imageNamed:@"jubao_button"] forState:UIControlStateNormal];
                    [button setImage:[UIImage imageNamed:@"btn_seleced"] forState:UIControlStateSelected];
                    [button setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
                    [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
                    button.tag=i+1;
                    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
                    [self.view addSubview:button];
                }

-(void)buttonClicked:(UIButton *)btn
{
    NSLog(@"%d",btn.isSelected);
    btn.selected=!btn.selected;
}

在另一个方法里获取选中的按钮项:

NSString * r_id=@"";
    for (int i=0; i<array.count; i++) {
        UIButton * button=(UIButton *)[self.view viewWithTag:i+1];
        if (button.isSelected==YES) {
            [idArray addObject:array[i][@"id"]];
            if ([r_id isEqualToString:@""]) {
                r_id=array[i][@"id"];
            }else{
            r_id=[NSString stringWithFormat:@"%@@%@",r_id,array[i][@"id"]];
            }
        }
    }
收藏
0
sina weixin mail 回到顶部