how to set hex color code for background [duplicate]

I like to use this little piece of code to use HTML web colors in my apps. Usage: [self.view setBackgroundColor: [self colorWithHexString:@”FFFFFF”]]; /* white */ The Code: -(UIColor*)colorWithHexString:(NSString*)hex { NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be 6 or 8 characters if ([cString length] < 6) return [UIColor grayColor]; // strip 0X … Read more

How to create a hex color string UIColor initializer in Swift? [duplicate]

Xcode 9 • Swift 4 or later extension UIColor { convenience init?(hexaRGB: String, alpha: CGFloat = 1) { var chars = Array(hexaRGB.hasPrefix(“#”) ? hexaRGB.dropFirst() : hexaRGB[…]) switch chars.count { case 3: chars = chars.flatMap { [$0, $0] } case 6: break default: return nil } self.init(red: .init(strtoul(String(chars[0…1]), nil, 16)) / 255, green: .init(strtoul(String(chars[2…3]), nil, 16)) … Read more

How to convert HEX RGB color codes to UIColor?

In some code of mine, I use 2 different functions: void SKScanHexColor(NSString * hexString, float * red, float * green, float * blue, float * alpha) { NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@”#” withString:@””]; if([cleanString length] == 3) { cleanString = [NSString stringWithFormat:@”%@%@%@%@%@%@”, [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)], [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)], [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString … Read more

Extract RGB Values From UIColor

You can convert UIColor to CIColor and then extract the color components from it as follow: Update: Xcode 8.3.2 • Swift 3.1 extension UIColor { var coreImageColor: CIColor { return CIColor(color: self) } var components: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { let coreImageColor = self.coreImageColor return (coreImageColor.red, coreImageColor.green, coreImageColor.blue, coreImageColor.alpha) } } … Read more

Programmatically create a UIView with color gradient

Objective-C: UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = view.bounds; gradient.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor blackColor].CGColor]; [view.layer insertSublayer:gradient atIndex:0]; Swift: let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 50)) let gradient = CAGradientLayer() gradient.frame = view.bounds gradient.colors = [UIColor.white.cgColor, UIColor.black.cgColor] view.layer.insertSublayer(gradient, at: 0) Info: … Read more

tech