【问题标题】:I am trying to change button colour in category file but there is no idea for me?我正在尝试更改类别文件中的按钮颜色,但我不知道?
【发布时间】:2015-07-15 05:26:01
【问题描述】:

这是将十六进制转换为 rgb 的代码。它工作正常。

 #import "UIButton+ButtonClr.h"

@implementation UIButton (ButtonClr)
- (UIColor *)colorFromHexString:(NSString *)hexString {
    unsigned rgbValue = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    [scanner setScanLocation:1]; /// bypass '#' character///
    [scanner scanHexInt:&rgbValue];
    return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];

}
-(void)btnclr
{
    UIButton *set1;//i tried many methods here but no use please provide some help full method


}
///set1.backgroundcolor=[self colorFromHexString:@"#ffc400" ];///here i am used this for changing the color but not working.

【问题讨论】:

  • 查看答案
  • 问题是什么?是否有适合您的想法?什么样的想法?你的标题没有意义,所以如果有问题的话,很难看出是什么问题。
  • @anthon OK 会保重的

标签: ios objective-c uibutton uicolor


【解决方案1】:

UIColor+HexColor.h

@interface UIColor (HexColor)
// send hex color codes array and it will return UIColor
+ (UIColor *)colorWithHexArray:(NSString *) color;
@end 

UIColor+HexColor.m

 + (UIColor *)colorWithHexArray:(NSString *) color
        {
            @try {
                NSMutableArray * rgbColorArray = [[NSMutableArray alloc] init];


                    NSString * colorArray1 = [NSString stringWithFormat:@"%@",color];

                    NSString *cString = [[colorArray1 stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];


                    // String empty then show clear color
                    if ([cString length]==0) return [UIColor clearColor];


                    // String should be 6 or 8 characters
                    if ([cString length] < 6) return [UIColor grayColor];

                    // strip 0X if it appears
                    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];

                    // # replace with blank space
                    if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];


                    if ([cString length] != 6) return  [UIColor grayColor];

                    // Separate into r, g, b substrings
                    NSRange range;
                    range.location = 0;
                    range.length = 2;
                    NSString *rString = [cString substringWithRange:range];

                    range.location = 2;
                    NSString *gString = [cString substringWithRange:range];

                    range.location = 4;
                    NSString *bString = [cString substringWithRange:range];

                    // Scan values
                    unsigned int r, g, b;
                    [[NSScanner scannerWithString:rString] scanHexInt:&r];
                    [[NSScanner scannerWithString:gString] scanHexInt:&g];
                    [[NSScanner scannerWithString:bString] scanHexInt:&b];

                    UIColor * color = [UIColor colorWithRed:((float) r / 255.0f)
                                                      green:((float) g / 255.0f)
                                                       blue:((float) b / 255.0f)
                                                      alpha:1.0f];

                    [rgbColorArray addObject:color];
                    return [rgbColorArray objectAtIndex:0];
                }

                return [UIColor clearColor];
            }
            @catch (NSException *exception) {
                NSLog(@"Exception %@",exception);
            }
            @finally {

            }
            return [UIColor clearColor];
        }

按如下方式调用该类别方法:

set1.backgroundColor =  [UIColor colorWithHexArray:@"#ff2323"];

我希望它能解决你的问题:)

【讨论】:

  • 这个参数是一个数组我怎么能作为一个字符串发送[UIColor colorWithHexArray:@"#ff2323"];
  • 这样发送@[@"ff2323"],它只是为将来如果我们想显示渐变色做准备,然后会更新一些代码。所以现在你只需在数组中传递它
  • 嘿,我已经根据您的要求更新了我的答案,现在它需要字符串参数,请检查并告诉我它是否有效
  • Kishore 如果它对你有用,请你接受并投票给我的答案
  • 这是 uicolor 类别,但我要求按钮,有什么想法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 2020-12-15
  • 1970-01-01
  • 2020-11-28
相关资源
最近更新 更多