我可以使用“png”文件创建图像。
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Operation"
image:[UIImage imageNamed:@"plus.png"] tag:0 ];
但我想使用unicode图像(721)或字符val,即:
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Operation"
image: {unicode: 721 }];
我该怎么做? THX
答案 0 :(得分:3)
我已经创建了一个小帮助方法,可以从NSString
创建一个通常包含单个Unicode字符的小图像。
+ (UIImage *)characterImage:(NSString *)ch {
UIFont *font = [UIFont boldSystemFontOfSize:12];
CGSize chsize = [ch sizeWithAttributes:@{ NSFontAttributeName : font }];
CGSize imgsize = CGSizeMake(16, 16);
UIGraphicsBeginImageContextWithOptions(imgsize, NO, [UIScreen mainScreen].scale);
CGRect rect = CGRectMake((imgsize.width - chsize.width) / 2.0, (imgsize.height - chsize.height) / 2.0, chsize.width, chsize.height);
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
pStyle.alignment = NSTextAlignmentCenter;
pStyle.lineBreakMode = NSLineBreakByClipping;
NSDictionary *attrs = @{ NSFontAttributeName : font, NSParagraphStyleAttributeName : pStyle };
[ch drawInRect:rect withAttributes:attrs];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
调整字体大小和图像大小以满足您的需求。
然后你可以这样做:
NSString *tabChar = @"✚"; // Unicode U+271A
UIImage *tabImg = [SomeHelperClass characterImage:tabChar];
根据您添加方法的位置调整为characterImage:
的号码。