iOS中将文本内容实现3D效果
2012-12-28 15:48
246 查看
在iOS中可以将文本实现3D效果,方法是,不断的重复画文本的layer,创建有层次的效果:
我是创建UIImage Category,命名为UIImage+3d,更多详细的解决方案见http://ask.csdn.net/questions/781
.h文件:
.m文件:
导入category扩展然后如下使用:
我是创建UIImage Category,命名为UIImage+3d,更多详细的解决方案见http://ask.csdn.net/questions/781
.h文件:
// // UIImage+3D.h // // Created by Lefteris Haritou on 12/10/12. // Feel Free to use this code, but please keep the credits // #import <UIKit/UIKit.h> @interface UIImage (Extensions) + (UIImage *)create3DImageWithText:(NSString *)_text Font:(UIFont*)_font ForegroundColor:(UIColor*)_foregroundColor ShadowColor:(UIColor*)_shadowColor outlineColor:(UIColor*)_outlineColor depth:(int)_depth; @end
.m文件:
// // UIImage+3D.m // // Created by Lefteris Haritou on 12/10/12. // Feel Free to use this code, but please keep the credits // #import "UIImage+3D.h" @implementation UIImage (Extensions) + (UIImage *)create3DImageWithText:(NSString *)_text Font:(UIFont*)_font ForegroundColor:(UIColor*)_foregroundColor ShadowColor:(UIColor*)_shadowColor outlineColor:(UIColor*)_outlineColor depth:(int)_depth { //calculate the size we will need for our text CGSize expectedSize = [_text sizeWithFont:_font constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT)]; //increase our size, as we will draw in 3d, so we need extra space for 3d depth + shadow with blur expectedSize.height+=_depth+5; expectedSize.width+=_depth+5; UIColor *_newColor; UIGraphicsBeginImageContextWithOptions(expectedSize, NO, [[UIScreen mainScreen] scale]); CGContextRef context = UIGraphicsGetCurrentContext(); //because we want to do a 3d depth effect, we are going to slightly decrease the color as we move back //so here we are going to create a color array that we will use with required depth levels NSMutableArray *_colorsArray = [[NSMutableArray alloc] initWithCapacity:_depth]; CGFloat *components = (CGFloat *)CGColorGetComponents(_foregroundColor.CGColor); //add as a first color in our array the original color [_colorsArray insertObject:_foregroundColor atIndex:0]; //create a gradient of our color (darkening in the depth) int _colorStepSize = floor(100/_depth); for (int i=0; i<_depth; i++) { for (int k=0; k<3; k++) { if (components[k]>(_colorStepSize/255.f)) { components[k]-=(_colorStepSize/255.f); } } _newColor = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:CGColorGetAlpha(_foregroundColor.CGColor)]; //we are inserting always at first index as we want this array of colors to be reversed (darkest color being the last) [_colorsArray insertObject:_newColor atIndex:0]; } //we will draw repeated copies of our text, with the outline color and foreground color, starting from the deepest for (int i=0; i<_depth; i++) { //change color _newColor = (UIColor*)[_colorsArray objectAtIndex:i]; //draw the text CGContextSaveGState(context); CGContextSetShouldAntialias(context, YES); //draw outline if this is the last layer (front one) if (i+1==_depth) { CGContextSetLineWidth(context, 1); CGContextSetLineJoin(context, kCGLineJoinRound); CGContextSetTextDrawingMode(context, kCGTextStroke); [_outlineColor set]; [_text drawAtPoint:CGPointMake(i, i) withFont:_font]; } //draw filling [_newColor set]; CGContextSetTextDrawingMode(context, kCGTextFill); //if this is the last layer (first one we draw), add the drop shadow too and the outline if (i==0) { CGContextSetShadowWithColor(context, CGSizeMake(-2, -2), 4.0f, _shadowColor.CGColor); } else if (i+1!=_depth){ //add glow like blur CGContextSetShadowWithColor(context, CGSizeMake(-1, -1), 3.0f, _newColor.CGColor); } [_text drawAtPoint:CGPointMake(i, i) withFont:_font]; CGContextRestoreGState(context); } UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return finalImage; }
导入category扩展然后如下使用:
UIImage *my3dImage = [UIImage create3DImageWithText:@"3" Font:[UIFont systemFontOfSize:250] ForegroundColor:[UIColor colorWithRed:(200/255.f) green:(200/255.f) blue:(200/255.f) alpha:1.0] ShadowColor:[UIColor blackColor] outlineColor:[UIColor colorWithRed:(225/255.f) green:(225/255.f) blue:(225/255.f) alpha:1.0] depth:8]; UIImageView *imgView = [[UIImageView alloc] initWithImage:my3dImage]; [self.view addSubview: imgView];
图片效果:
相关文章推荐
- 如何让IOS中的文本实现3D效果
- IOS 实现3D 浮动效果动画
- 实现textbox对输入内容的自动检索功能,效果相当于百度首页的文本输入框
- iOS中实现获取文本内容的宽高
- android TextView 文本内容收起,展开的效果实现
- ios中开发在cell上实现内容的动画滑动效果
- JS实现文本内容上下滚动效果
- OAF 中对文字实现html效果及对超级长文本实现默认换行,对只读的messageTextInput中的内容自动换行
- android 实现在文本内容超过固定宽度可手动左右滚动查看效果
- 文本内容超过TextView的长度时,实现文本内容水平滚动效果
- 仿IOS特效(一)——Android 自定义View实现3D滚轮效果的城市联动选择器
- ios中实现3D效果--雪花飘的效果
- android 实现在文本内容超过固定宽度可手动左右滚动查看效果
- CSS3实现的文本3D效果附图
- Codrops 教程:实现内容倾斜的 3D 幻灯片效果
- 实现文本内容超出文本框时出现省略号的效果。
- iOS UIWebView 获取内容实际高度,关闭滚动效果
- 关于ios中的点赞控件效果的实现--UIControl
- iOS 7中实现模糊效果