So the text of my playingCardLabel is @"A \u2663"

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

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

How come when I tap the button, only the A changes to white, the spade (or whatever it is called) remains black no matter what?

有帮助吗?

解决方案

Those symbols appear to be fixed in color. Use \U2663 for the black club (♣︎) and use \U2667 for the white club (♧). There are similar black or white version of the spade, heart, and diamond.

其他提示

create image from symbol of unicode using your colour. use UIImageView instead UILabel, or use UILabel with NSAttributedString + NSTextAttachment (image inside).

@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];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top