要是能把 emoji 当成 tabbar 的图标就好了

2015-09-21 15:54:09 +08:00
 machinemxy

明明 emoji 里已经有很多优秀的图标了,但是并不能直接把它作为 tabbar 的 image ,还得自己画完再利用工具变为 x1,x2,x3 的尺寸。感觉 emoji 那么多设计精美的图标,应该让它能过发挥更大的作用的。

2878 次点击
所在节点    iDev
4 条回复
ibremn
2015-09-21 16:03:18 +08:00
+ (UIImage *)imageWithEmoji:(NSString *)emoji size:(CGFloat )size {
if (emoji.length == 0 ) return nil;
if (size < 1 ) return nil;

CGFloat scale = [UIScreen mainScreen].scale;
CTFontRef font = CTFontCreateWithName (CFSTR ("AppleColorEmoji"), size * scale, NULL );
if (!font ) return nil;

NSAttributedString *str = [[NSAttributedString alloc] initWithString:emoji attributes:@{ (__bridge id )kCTFontAttributeName:(__bridge id )font, (__bridge id )kCTForegroundColorAttributeName:(__bridge id )[UIColor clearColor].CGColor }];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
CGContextRef ctx = CGBitmapContextCreate (NULL, size * scale, size * scale, 8, 0, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst );
CGContextSetInterpolationQuality (ctx, kCGInterpolationHigh );
CTLineRef line = CTLineCreateWithAttributedString ((__bridge CFTypeRef )str );
CGRect bounds = CTLineGetBoundsWithOptions (line, kCTLineBoundsUseGlyphPathBounds );
CGContextSetTextPosition (ctx, 0, -bounds.origin.y );
CTLineDraw (line, ctx );
CGImageRef imageRef = CGBitmapContextCreateImage (ctx );
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];

CFRelease (font );
CGColorSpaceRelease (colorSpace );
CGContextRelease (ctx );
if (line )CFRelease (line );
if (imageRef ) CFRelease (imageRef );

return image;
}

加个 Category 到 UIImage 上,随时能把 Emoji 转为 Image ,性能不比直接读取图片差多少。。
UIImage *image = [UIImage imageWithEmoji:@"😄" size:80];
machinemxy
2015-09-21 19:37:54 +08:00
@ibremn 谢谢!十分好的想法!
fhefh
2015-09-21 20:50:17 +08:00
mark 收藏~
parkerjj
2015-09-22 10:45:19 +08:00
马克一下。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/222471

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX