【问题标题】:how to set a hex color to textfield border in ios如何在ios中为文本框边框设置十六进制颜色
【发布时间】:2013-05-16 10:31:12
【问题描述】:
UIColor *bgcolour = [BackgroundLayer colorWithHexString:@"F13982"];
textField.layer.borderColor=[[UIColor colorWithCGColor:(__bridge CGColorRef)(bgcolour)] CGColor];

谁能说出如何将 UIColor 对象“bgcolor”设置为 Textfield 边框?

【问题讨论】:

  • 也将您的问题标记为目标 c

标签: iphone ios uitextfield border-color


【解决方案1】:
textField.layer.borderColor= bgcolour.CGColor;

【讨论】:

    【解决方案2】:

    首先你可以使用这个函数得到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];
    

    【讨论】:

      【解决方案3】:

      您遵循以下步骤:

      1. 将十六进制值转换为 RGB 使用 HexToRGB
      2. textfield.layer.borderColor=[UIColor colorWithRed:244/255.00f green:133/255.00f blue:116/255.00f alpha:1.0f];

      【讨论】:

      • [UIColor ].CGColor 用于图层颜色
      【解决方案4】:

      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
      

      UIColor+HexColor.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];
      

      @usage 2 (#eeeeee):

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-26
        • 2017-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多