像spade这样的Unicode字符不会改变颜色? (目标c)

时间:2014-04-25 04:48:37

标签: ios unicode uilabel

所以我的playCardLabel的文字是@“ A \ u2663

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

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

当我点击按钮时,只有A变为白色,铲子(或其他任何东西)无论如何都保持黑色?

2 个答案:

答案 0 :(得分:2)

这些符号看起来是固定的颜色。使用\U2663表示黑人俱乐部(♣♣),并使用\U2667表示白人俱乐部(♧)。有类似的黑色或白色版本的铁锹,心脏和钻石。

答案 1 :(得分:0)

使用您的颜色从unicode符号创建图像。使用UIImageView代替UILabel,或者使用UILabel和NSAttributedString + NSTextAttachment(图片里面)。

@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];