ios 将背景颜色设置为 RGB UIColor ButtonTypeRoundedRect

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20016955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:23:17  来源:igfitidea点击:

Set Background Color to RGB UIColor ButtonTypeRoundedRect

iosobjective-cuibuttonuicolor

提问by maxhud

I've been looking all over the place for the past half hour trying to find a way to create a button with round corners that can have a custom rgb background...

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

This is what I have currently, but clearly it doesn't work:

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

- (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;
}

I've seen a bunch of stuff about how you can't set the background color on a roundedRect and how to fix it but I need to be able to dynamically generate the color as you can see in the code...

我已经看到了很多关于如何无法在 roundedRect 上设置背景颜色以及如何修复它的内容,但我需要能够动态生成颜色,如您在代码中看到的那样......

Currently it just doesn't have a background color for the buttons. Interestingly, if I use [UIColor colorColor]it works just fine, it's just the RGB that throws it off.

目前它只是没有按钮的背景颜色。有趣的是,如果我使用[UIColor colorColor]它工作得很好,它只是 RGB 把它扔掉。

How can I go about this in my situation specifically?

在我的情况下,我该如何解决这个问题?

回答by Hardik Thakkar

You need to do like thisFirst take RGB codes value from Color Palettefrom this you will get three different code for red , green and bluethen to apply the color to your Control

您需要这样做首先从调色板中获取 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];

here in code 255,50,60 is a RGB code Value

这里的代码 255,50,60 是一个 RGB 代码值

回答by Wain

When you create your colour the components each need to be in the range 0 to 1. Yours look to be up to 255 so you need to divide them all by 255.0 (apart from the alpha which you have correct).

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

回答by Vizllx

This will surely work

这肯定会奏效

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];

回答by Parthi

No sure about your RGB value but divide by 255

不确定您的 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];

回答by IKKA

You need to do like this

你需要这样做

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];

回答by mraaroncruz

Here's my Swift solution ripped off from one of these previous answers.
For avant-garde devs :P

这是我从这些以前的答案之一中提取的 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