iOS UIButton设置button上的文字和图片

这个是最后的效果图,是一个滚动选择的一个效果,因为这里之前直接放了button,后来又说要加图片,废话不多说,上代码。

1-1.png
// 借款理由
for (int i = 0; i < _borrowingreasons.count; i++) {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = i + 100;
    btn.frame = CGRectMake(18 + 128 * i, 0, 110, 70);
    [btn setTitle:_borrowingreasons[i] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    btn.backgroundColor     = [UIColor whiteColor];
    btn.layer.cornerRadius  = 4;
    btn.clipsToBounds       = YES;
    btn.layer.borderWidth   = 1;
    btn.layer.borderColor   = RGB(213, 213, 213).CGColor;
    btn.titleLabel.font     = [UIFont systemFontOfSize:15];
    [btn addTarget:self action:@selector(chooseBorrowingReasons:) forControlEvents:UIControlEventTouchUpInside];
    if (0 == i) {
        btn.backgroundColor = RGB(251, 218, 68);
        [btn addSubview:applyreasonsImageView];
        [users setObject:@"付生活费" forKey:@"borrowtype"];

    }
    // btn's image
    [btn setImage:[UIImage imageNamed:_borrowingreasons_pic[i]] forState:UIControlStateNormal];
    btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 43, 0, 43);
    btn.titleEdgeInsets = UIEdgeInsetsMake(30, -25, 0, 0);
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    [_borrowingreasonsScrollView addSubview:btn];
}

参考文章:http://doc.okbase.net/willingYaTou/archive/38295.html

你可能感兴趣的:(iOS UIButton设置button上的文字和图片)