将背景颜色设置为RGB UIColor ButtonTypeRoundedRect

时间:2013-11-16 09:42:20

标签: ios objective-c uibutton uicolor

过去半小时我一直在寻找一个方法,试图找到一个方法来创建一个圆角的按钮,可以有一个自定义的rgb背景......

这就是我目前所拥有的,但显然它不起作用:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.separatorColor = [UIColor clearColor];

    NSString *CellIdentifier = @"TimesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //[cell sizeToFit];


    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self
               action:@selector(customActionPressed:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Custom Action" forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(-10.0f, 0.0f, 0.0f, 0.0f)];

    button.frame = CGRectMake(5, 5, 310, 50);

    int r = 255-1;
    int g = 255-180;
    int b = 7-1;

    NSLog(@"%i", 180+(int)((g/5)*(indexPath.row+1)));

    UIColor *thisColor = [UIColor colorWithRed:1+(int)((r/5)*(indexPath.row+1)) green:180+(int)((g/5)*(indexPath.row+1)) blue:1+(int)((b/5)*(indexPath.row+1)) alpha:1];

    button.backgroundColor = thisColor;
    button.clipsToBounds = YES;
    button.layer.cornerRadius = 15;


    [cell addSubview:button];

    return cell;
}

我见过很多关于如何在roundRect上设置背景颜色以及如何修复它的东西,但是我需要能够动态生成颜色,就像你在代码中看到的一样......

目前它没有按钮的背景颜色。有趣的是,如果我使用[UIColor colorColor]它可以正常工作,它只是将它抛出的RGB。

我怎样才能在具体情况下解决这个问题?

6 个答案:

答案 0 :(得分:12)

你需要这样做 首先从调色板中获取RGB代码值,您将获得红色,绿色和蓝色的三种不同代码,然后将颜色应用于控件

UIButton *customButton = [UIButton buttonWithType: UIButtonTypeCustom];
[customButton setFrame:CGRectMake(60, 100, 200, 40)];

[customButton setBackgroundColor: [UIColor colorWithRed:255/255.0f green:50/255.0f blue:60/255.0f alpha:1.0f]];

[customButton setTitleColor:[UIColor blackColor] forState:
 UIControlStateHighlighted];
[customButton setTitle:@"Custom Button" forState:UIControlStateNormal];
[self.view addSubview:customButton];
代码255,50,60中的

是RGB代码值

答案 1 :(得分:10)

当你创建颜色时,每个组件都需要在0到1的范围内。你看起来要高达255,所以你需要将它们全部除以255.0(除了正确的alpha)。

答案 2 :(得分:3)

这肯定有用

UIButton* button = [[UIButton alloc]initWithFrame:f];
button.layer.cornerRadius = 8;
button.layer.borderWidth = 1;
button.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:1];
button.layer.borderColor = [UIColor grayColor].CGColor;
button.clipsToBounds = YES;
[self.view addSubview:button];

答案 3 :(得分:1)

不确定您的RGB值,但除以255

UIColor *thisColor = [UIColor colorWithRed:(1+(int)((r/5)*(indexPath.row+1))/255) green:(180+(int)((g/5)*(indexPath.row+1))/255) blue:(1+(int)((b/5)*(indexPath.row+1))/255) alpha:1];

答案 4 :(得分:0)

你需要这样做

UIColor *thisColor = [UIColor colorWithRed:(1+(int)((r/5)*(indexPath.row+1)))/225.f green:(180+(int)((g/5)*(indexPath.row+1)))/255.f blue:(1+(int)((b/5)*(indexPath.row+1)))/255.f alpha:1.0f];

答案 5 :(得分:0)

在这里,我的Swift解决方案从之前的答案中脱颖而出 对于前卫的开发者:P

import Foundation

struct ColorUtils {
    static func hexToUIColor (hex: String, alpha: CGFloat = 1.0) -> UIColor {
        var rgbValue: UInt32 = 0
        var scanner = NSScanner(string: hex)
        scanner.scanLocation = 1
        scanner.scanHexInt(&rgbValue)
        let red = (CGFloat((rgbValue & 0xFF0000) >> 16)) / 255.0
        let green = (CGFloat((rgbValue & 0xFF00) >> 8)) / 255.0
        let blue = CGFloat((rgbValue & 0xFF)) / 255.0
        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    } 
}

// Use:
// ColorUtils.hexToUIColor("#FF0000") // you can optionally add alpha as second arg