ヘックスRGBカラーコードをuicolorに変換する方法は?
質問
nsstringとして#ffffffのようなRGBヘックスコードがあり、それをuicolorに変換したいと考えています。それを行う簡単な方法はありますか?
解決
私のコードで, 、2つの異なる機能を使用します。
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 substringWithRange:NSMakeRange(2, 1)]];
}
if([cleanString length] == 6) {
cleanString = [cleanString stringByAppendingString:@"ff"];
}
unsigned int baseValue;
[[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
if (red) { *red = ((baseValue >> 24) & 0xFF)/255.0f; }
if (green) { *green = ((baseValue >> 16) & 0xFF)/255.0f; }
if (blue) { *blue = ((baseValue >> 8) & 0xFF)/255.0f; }
if (alpha) { *alpha = ((baseValue >> 0) & 0xFF)/255.0f; }
}
そして、私はこのようにそれを使用します:
UIColor * SKColorFromHexString(NSString * hexString) {
float red, green, blue, alpha;
SKScanHexColor(hexString, &red, &green, &blue, &alpha);
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
これをaとして使用したい場合 UIColor
カテゴリ、それは数行を変更するだけの問題です。
+ (UIColor *) colorFromHexString:(NSString *)hexString {
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 substringWithRange:NSMakeRange(2, 1)]];
}
if([cleanString length] == 6) {
cleanString = [cleanString stringByAppendingString:@"ff"];
}
unsigned int baseValue;
[[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
float red = ((baseValue >> 24) & 0xFF)/255.0f;
float green = ((baseValue >> 16) & 0xFF)/255.0f;
float blue = ((baseValue >> 8) & 0xFF)/255.0f;
float alpha = ((baseValue >> 0) & 0xFF)/255.0f;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
これにより、「#ABC」、「#ABCDEF31」などの文字列が処理されます。
他のヒント
ヘックス値を使用している場合。
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//Then use any Hex value
self.view.backgroundColor = UIColorFromRGB(0xD2691E);
私は簡単な解決策を探していて、これを思いつきました(完全に客観的なCではありませんが、魅力のように機能します):
NSString *stringColor = @"#AABBCC";
NSUInteger red, green, blue;
sscanf([stringColor UTF8String], "#%02X%02X%02X", &red, &green, &blue);
UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
Uicolorと呼ばれる素晴らしいカテゴリがあります 「uicolor+拡張」 RGB 16進ストリングからUicolorを取得するクラスメソッドがあります。
使いやすいです:
UIColor *myColor = [UIColor colorWithHexString:@"FF0000"];
さらに、他の多くの潜在的に有用なユーティリティをuicolorに追加します。詳細情報を入手できます この記事.
簡単に、このWebサイトにアクセスして、16進価値を入力してください。 http://www.corecoding.com/utilities/rgb-or-hex-to-float.php
+ (UIColor *)colorWithHexString:(NSString *)colorString
{
colorString = [colorString stringByReplacingOccurrencesOfString:@"#" withString:@""];
if (colorString.length == 3)
colorString = [NSString stringWithFormat:@"%c%c%c%c%c%c",
[colorString characterAtIndex:0], [colorString characterAtIndex:0],
[colorString characterAtIndex:1], [colorString characterAtIndex:1],
[colorString characterAtIndex:2], [colorString characterAtIndex:2]];
if (colorString.length == 6)
{
int r, g, b;
sscanf([colorString UTF8String], "%2x%2x%2x", &r, &g, &b);
return [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0];
}
return nil;
}
フォーマット#123、123、#fff195、fff195
+ (UIColor *)colorWithHexValue:(int)hexValue
{
float red = ((hexValue & 0xFF0000) >> 16)/255.0;
float green = ((hexValue & 0xFF00) >> 8)/255.0;
float blue = (hexValue & 0xFF)/255.0;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
フォーマット0xfff195の場合
私が見つけた最も簡単な方法: ヘックスからUicolorコンバーター
「#」なしで16進番号を入力するだけで、uicolorコードを返します。たとえば、オレンジ色のコード(#f77f00)は次のとおりです。
[UIColor colorWithRed:0.969 green:0.498 blue:0 alpha:1.0]
6文字を3つのペアに分割してから、それを小数に変換し、それを255で割って各カラーコンポーネントをフロートとして取得します。
その後、コンポーネントを次のように渡すことができます。
[UIColor colorWithRed: green: blue: alpha:1];
カスタムメソッドまたはプラグインを使用するのが不便な場合のために、SwiftおよびObjective-Cのuicolorコードスニペットに16進コードを即座に変換するオンラインツールを作成しました。 https://iosref.com/uihex/
上記のすべてのコードを書きたくない場合は、このサイトを確認できます。 http://www.diovo.com/apps/rgb-to-uicolor-nverter.html
このようなヘックス色から:#ffffff、サイトは次のような文字列でそれを変換します:
UIColor *aColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.000];
ObjectivecからSwiftに変換することがあなたにとって難しい場合、Swiftの答えは次のとおりです。現在、#なしで文字列を使用するだけですが、スキャナーメソッドを追加してスキップできます。
func stringToColor(stringColor: String) -> UIColor {
var hexInt: UInt32 = 0
let scanner = NSScanner(string: stringColor)
scanner.scanHexInt(&hexInt)
let color = UIColor(
red: CGFloat((hexInt & 0xFF0000) >> 16)/255,
green: CGFloat((hexInt & 0xFF00) >> 8)/255,
blue: CGFloat((hexInt & 0xFF))/255,
alpha: 1)
return color
}
あなたにはオプションがあることを忘れないでください 16進値をRGBに変換します インターフェイスビルダーに入力します。コードの行をいくつか保存します。
これはここで少し遅れていると思います...しかし、私はこのバージョンが非常にエレガントな方法でそれを行うホワイトハウスGithubリポジトリで見つけました:
+(UIColor *)colorFromRGBHexString:(NSString *)colorString {
if(colorString.length == 7) {
const char *colorUTF8String = [colorString UTF8String];
int r, g, b;
sscanf(colorUTF8String, "#%2x%2x%2x", &r, &g, &b);
return [UIColor colorWithRed:(r / 255.0) green:(g / 255.0) blue:(b / 255.0) alpha:1.0];
}
return nil;
}
ヘックス値を使用する場合、さらに簡単な方法があると思います。ファイルの上部に定義を追加するか、変換用のヘッダーファイル(uicolorfromrgb)を参照するだけです。固定されたヘックス色の値のテンプレートを追加することもできます。
#define CLR_YELLOW_TEXT 0xf4dc89 // A Light Yellow text
#define CLR_GREEN_TEXT 0x008040 // Dark Green text for my buttons
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
次に、HEX値または定義されたhex値を使用してコードで参照してください。例えば...
[myButton1 setTitleColor:UIColorFromRGB(0xd02d2d) forState:UIControlStateNormal];
[myButton2 setTitleColor:UIColorFromRGB(CLR_GREEN_TEXT) forState:UIControlStateNormal];
[myButton3 setTitleColor:UIColorFromRGB(CLR_YELLOW_TEXT) forState:UIControlStateNormal];
(PS-これは1.0のアルファを想定していますが、定義では常に変更できます)。
楽しみ。
「#rrggbb」値を使用してuicolorを作成するのに非常に役立つココアポッドライブラリが非常に役立つことがわかりました。
pod 'UIColor-HexRGB'
+(UIColor*)colorWithHexString:(NSString*)hexString
{
NSString *cString = [[hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([cString length] < 6) return [UIColor grayColor];
if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
if ([cString length] != 6) return [UIColor grayColor];
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];
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}
最終的にカテゴリを作成しました UIColor
他のプロジェクトで再利用できること。
Objective-C
#import <UIKit/UIKit.h>
@interface UIColor (HexColor)
+ (UIColor *)colorFromHex:(unsigned long)hex;
@end
@implementation UIColor (HexColor)
+ (UIColor *)colorFromHex:(unsigned long)hex
{
return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0 green:((float)((hex & 0xFF00) >> 8))/255.0 blue:((float)(hex & 0xFF))/255.0 alpha:1.0];
}
@end
// USAGE
UIColor *customRedColor = [UIColor colorFromHex:0x990000];
迅速
extension UIColor {
convenience init(hex: Int) {
let red = CGFloat((hex & 0xff0000) >> 16) / 255.0
let green = CGFloat((hex & 0x00ff00) >> 8) / 255.0
let blue = CGFloat(hex & 0x0000ff) / 255.0
self.init(red: red, green: green, blue: blue, alpha: 1.0)
}
}
// USAGE
let customColor = UIColor(hex: 0x990000)