2014-04-25 20 views
0

所以我playingCardLabel的文本是@「A \ u2663Unicode字符(如黑桃不變色)? (目標C)

- (IBAction)flipCardButtonTapped:(UIButton *)sender 
{ 
    if([self.playingCardLabel.textColor isEqual:[UIColor whiteColor]]) 
     self.playingCardLabel.textColor = [UIColor blackColor]; 

    else 
     self.playingCardLabel.textColor = [UIColor whiteColor]; 
} 

爲什麼當我點擊該按鈕,只有A變爲白色,鐵鍬(或者不管它是什麼叫什麼)不管是什麼都保持黑色?

回答

2

這些符號似乎是固定的顏色。黑色俱樂部使用\U2663(♣︎),白色俱樂部使用\U2667(♧)。有類似的黑色或白色版本的鍬,心臟和鑽石。

+0

感謝您對unicode的澄清。所以我發現只有像黑人俱樂部那樣的人物有很多。以下是我發現的工作:♣︎ 您可以將其更改爲您希望的任何顏色。 – user3451821

+0

如何將unicode文本更改爲任何顏色? – Sabareesh

0

使用您的顏色從unicode的符號創建圖像。使用UIImageView而不是UILabel,或者使用帶有NSAttributedString + NSTextAttachment的UILabel(圖像在裏面)。

@implementation NSString (OSExt) 

- (UIImage *)toImageWithAttributes:(NSDictionary*)attributes 
{ 
    CGSize size = [self sizeWithAttributes:attributes]; 
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); 
    [self drawAtPoint:CGPointZero withAttributes:attributes]; 
    CGContextRef imgctx = UIGraphicsGetCurrentContext(); 
    // use blend mode to fill symbols like @"♣︎" 
    CGContextSetBlendMode(imgctx, kCGBlendModeSourceAtop); 
    CGContextSetFillColorWithColor(imgctx, [attributes[NSForegroundColorAttributeName] CGColor]); 
    CGContextFillRect(imgctx, (CGRect){CGPointZero, size}); 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image; 
} 

@end 

// use 
id attrs = @{NSForegroundColorAttributeName:[UIColor greenColor]}; 
UIImage *image = [@"♣︎" toImageWithAttributes:attrs];