使用您的顏色從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];
感謝您對unicode的澄清。所以我發現只有像黑人俱樂部那樣的人物有很多。以下是我發現的工作:♣︎ 您可以將其更改爲您希望的任何顏色。 – user3451821
如何將unicode文本更改爲任何顏色? – Sabareesh