如何在ios中设置文本框的十六进制颜色

ma8fv8wu  于 2023-02-01  发布在  iOS
关注(0)|答案(4)|浏览(117)
UIColor *bgcolour = [BackgroundLayer colorWithHexString:@"F13982"];
textField.layer.borderColor=[[UIColor colorWithCGColor:(__bridge CGColorRef)(bgcolour)] CGColor];

有人能告诉我们如何将UIColor对象“bgcolor”设置为Textfield边框吗?

a2mppw5e

a2mppw5e1#

textField.layer.borderColor= bgcolour.CGColor;
ar5n3qh5

ar5n3qh52#

首先,您可以使用此函数获取UIColorHex字符串

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

要更改边框颜色,请尝试以下操作

UITextField *theTextFiels=[[UITextField alloc]initWithFrame:CGRectMake(40, 40, 150, 30)];
        theTextFiels.borderStyle=UITextBorderStyleNone;
        theTextFiels.layer.cornerRadius=8.0f;
        theTextFiels.layer.masksToBounds=YES;
            theTextFiels.backgroundColor=[UIColor redColor];
        theTextFiels.layer.borderColor=[[UIColor blackColor]CGColor];
        theTextFiels.layer.borderWidth= 1.0f;

        [self.view addSubview:theTextFiels];
        [theTextFiels release];
cbeh67ev

cbeh67ev3#

请执行以下步骤:
1.使用HexToRGB将十六进制值转换为RGB。

  1. textfield.layer.borderColor=[UIColor colorWithRed:244/255.00f green:133/255.00f blue:116/255.00f alpha:1.0f];
ttygqcqt

ttygqcqt4#

UIColor+HexColor.h

//
//  UIColor+HexColor.h
//

#import <Foundation/Foundation.h>

@interface UIColor (HexColor)

//(int) color = RGBA hexadecimal - 8 hexadecimal digits, specifying 8 bits each of red, green, and blue, followed by 8 bits of alpha
//@usage: UIColor *myColor = [UIColor colorWithHex:0xEEEEEEff];
+ (UIColor* ) colorWithHex:(int)color;
@end

UI颜色+十六进制颜色.m:

//
//  UIColor+HexColor.m
//

#import "UIColor+HexColor.h"

@implementation UIColor (HexColor)

+ (UIColor* ) colorWithHex:(int)color
{

    float red = (color & 0xff000000) >> 24;
    float green = (color & 0x00ff0000) >> 16;
    float blue = (color & 0x0000ff00) >> 8;
    float alpha = (color & 0x000000ff);

    return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha/255.0];
}
@end

@用法1(编号F13982):

textField.layer.borderColor = [UIColor colorWithHex:0xF13982ff];

@用法2(#eeeeee):

UIColor *myColor = [UIColor colorWithHex:0xEEEEEEff];

相关问题