我想從十六進制轉換爲RGB,但十六進制處理像#FFFFFF這樣的字符串。我怎樣才能做到這一點?如何將十六進制轉換爲RGB
25
A
回答
81
我剛剛爲您擴展了我的UIColor類別。
使用它像UIColor *green = [UIColor colorWithHexString:@"#00FF00"];
//
// UIColor_Categories.h
//
// Created by Matthias Bauch on 24.11.10.
// Copyright 2010 Matthias Bauch. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface UIColor(MBCategory)
+ (UIColor *)colorWithHex:(UInt32)col;
+ (UIColor *)colorWithHexString:(NSString *)str;
@end
//
// UIColor_Categories.m
//
// Created by Matthias Bauch on 24.11.10.
// Copyright 2010 Matthias Bauch. All rights reserved.
//
#import "UIColor_Categories.h"
@implementation UIColor(MBCategory)
// takes @"#123456"
+ (UIColor *)colorWithHexString:(NSString *)str {
const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];
long x = strtol(cStr+1, NULL, 16);
return [UIColor colorWithHex:x];
}
// takes 0x123456
+ (UIColor *)colorWithHex:(UInt32)col {
unsigned char r, g, b;
b = col & 0xFF;
g = (col >> 8) & 0xFF;
r = (col >> 16) & 0xFF;
return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1];
}
@end
24
//In your header file
#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]
//usage
UIColor *color = UIColorFromRGB(0x000000)
//you can also use it inline
[text.textField setTextColor:UIColorFromRGB(0xcccccc)];
3
這可以幫助你
UIColor *organizationColor = [self colorWithHexString:@"#ababab" alpha:1];
- (UIColor *)colorWithHexString:(NSString *)str_HEX alpha:(CGFloat)alpha_range{
int red = 0;
int green = 0;
int blue = 0;
sscanf([str_HEX UTF8String], "#%02X%02X%02X", &red, &green, &blue);
return [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha_range];
}
相關問題
- 1. 將rgb轉換爲十六進制
- 2. 將RGB轉換爲十六進制
- 3. 將RGB轉換爲十六進制C++?
- 4. RGB十六進制轉換
- 5. RGB轉換爲十六進制的PHP
- 6. 將十進制轉換爲十六進制和十六進制
- 7. 將十六進制轉換爲二進制到十六進制?
- 8. RGB到十六進制轉換JavaScript
- 9. 如何十六進制轉換爲十六進制
- 10. 如何將十六進制NSData轉換爲十進制數組?
- 11. 如何將十六進制字符串轉換爲十進制?
- 12. 如何將14位十六進制轉換爲十進制
- 13. 如何在bash中將十進制轉換爲十六進制?
- 14. 如何將負十進制轉換爲十六進制?
- 15. 如何在Python中將十六進制轉換爲十進制?
- 16. 將字符串轉換爲十六進制到十六進制
- 17. 如何從十六進制轉換顏色爲RGB
- 18. 如何十六進制顏色轉換爲RGB顏色(24位)
- 19. 在R中將RGB十六進制轉換爲三維矩陣
- 20. 將RGB轉換爲十六進制顏色
- 21. 將十六進制顏色字符串轉換爲RGB顏色
- 22. 在ASP經典中將十六進制轉換爲RGB
- 23. 將RGB顏色值轉換爲十六進制
- 24. JavaScript將RGB整數轉換爲十六進制
- 25. 轉換爲十六進制?
- 26. 將十進制轉換爲十六進制/二進制
- 27. 將八進制數轉換爲十進制和十六進制
- 28. 將十六進制轉換爲二進制,然後轉換爲十進制
- 29. 如何將十六進制字符串轉換爲十六進制數
- 30. 如何將十六進制字符串轉換爲十六進制?
哪種語言? – st0le 2010-11-24 09:27:23
你能指定語言嗎? – 2010-11-24 09:32:13
對不起,我編輯它..它在客觀C – 2010-11-24 09:32:49