如何讓IOS中的文本實現3D效果

來源:互聯網
上載者:User

標籤:

本轉載至 http://bbs.aliyun.com/read/181991.html?spm=5176.7114037.1996646101.25.p0So7c&pos=9  
 

 zhedianshi層級: 幫幫團
發帖
487
雲幣
430
  • 加關注
  • 寫私信
 
只看樓主 更多操作樓主  發表於: 2014-06-10   
我想要在IOS中對一些文本進行3D效果渲染,使用了UIKit和標準視圖控制器。 
實現之的效果大概能成為這樣: 
 
 
能不能通過iOS和UIKit實現?我只用了一個靜態PNG圖片,常值內容根據使用者資料變化。
   關鍵詞: ios開發 3D效果第46期:北京雲棲大會、DingTalk、快速黑洞、碼農成長日記 分享到淘江湖新浪QQ微博QQ空間開心人人豆瓣網易微博百度鮮果白社會飛信回複引用舉報
 

 深白色層級: 小白
發帖
23
雲幣
45
  • 加關注
  • 寫私信
 
只看該作者沙發  發表於: 2014-06-10Re如何讓IOS中的文本實現3D效果  提供一個方法,不斷的重複畫文本的layer,建立有層次的效果: 
建立UIImage Category,命名為UIImage+3d.h檔案:複製代碼
  1. //
  2. //  UIImage+3D.h
  3. //
  4. //  Created by Lefteris Haritou on 12/10/12.
  5. //  Feel Free to use this code, but please keep the credits
  6. //
  7. #import <UIKit/UIKit.h>
  8. @interface UIImage (Extensions)
  9. + (UIImage *)create3DImageWithText:(NSString *)_text Font:(UIFont*)_font ForegroundColor:(UIColor*)_foregroundColor ShadowColor:(UIColor*)_shadowColor outlineColor:(UIColor*)_outlineColor depth:(int)_depth;
  10. @end
.m檔案: 
複製代碼
  1. //
  2. //  UIImage+3D.m
  3. //
  4. //  Created by Lefteris Haritou on 12/10/12.
  5. //  Feel Free to use this code, but please keep the credits
  6. //
  7. #import "UIImage+3D.h"
  8. @implementation UIImage (Extensions)
  9. + (UIImage *)create3DImageWithText:(NSString *)_text Font:(UIFont*)_font ForegroundColor:(UIColor*)_foregroundColor ShadowColor:(UIColor*)_shadowColor outlineColor:(UIColor*)_outlineColor depth:(int)_depth {
  10.     //calculate the size we will need for our text
  11.     CGSize expectedSize = [_text sizeWithFont:_font constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
  12.     //increase our size, as we will draw in 3d, so we need extra space for 3d depth + shadow with blur
  13.     expectedSize.height+=_depth+5;
  14.     expectedSize.width+=_depth+5;
  15.     UIColor *_newColor;
  16.     UIGraphicsBeginImageContextWithOptions(expectedSize, NO, [[UIScreen mainScreen] scale]);
  17.     CGContextRef context = UIGraphicsGetCurrentContext();
  18.     //because we want to do a 3d depth effect, we are going to slightly decrease the color as we move back
  19.     //so here we are going to create a color array that we will use with required depth levels
  20.     NSMutableArray *_colorsArray = [[NSMutableArray alloc] initWithCapacity:_depth];
  21.     CGFloat *components =  (CGFloat *)CGColorGetComponents(_foregroundColor.CGColor);
  22.     //add as a first color in our array the original color
  23.     [_colorsArray insertObject:_foregroundColor atIndex:0];
  24.     //create a gradient of our color (darkening in the depth)
  25.     int _colorStepSize = floor(100/_depth);
  26.     for (int i=0; i<_depth; i++) {
  27.         for (int k=0; k<3; k++) {
  28.             if (components[k]>(_colorStepSize/255.f)) {
  29.                 components[k]-=(_colorStepSize/255.f);
  30.             }
  31.         }        
  32.         _newColor = [UIColor colorWithRed:components[0] green:components[1] blue:components[2] alpha:CGColorGetAlpha(_foregroundColor.CGColor)];
  33.         //we are inserting always at first index as we want this array of colors to be reversed (darkest color being the last)
  34.         [_colorsArray insertObject:_newColor atIndex:0];
  35.     }
  36.     //we will draw repeated copies of our text, with the outline color and foreground color, starting from the deepest
  37.     for (int i=0; i<_depth; i++) {
  38.         //change color
  39.         _newColor = (UIColor*)[_colorsArray objectAtIndex:i];
  40.         //draw the text
  41.         CGContextSaveGState(context);
  42.         CGContextSetShouldAntialias(context, YES);        
  43.         //draw outline if this is the last layer (front one)
  44.         if (i+1==_depth) {
  45.             CGContextSetLineWidth(context, 1);
  46.             CGContextSetLineJoin(context, kCGLineJoinRound);
  47.             CGContextSetTextDrawingMode(context, kCGTextStroke);
  48.             [_outlineColor set];
  49.             [_text drawAtPoint:CGPointMake(i, i) withFont:_font];
  50.         }
  51.         //draw filling        
  52.         [_newColor set];
  53.         CGContextSetTextDrawingMode(context, kCGTextFill);
  54.         //if this is the last layer (first one we draw), add the drop shadow too and the outline
  55.         if (i==0) {
  56.             CGContextSetShadowWithColor(context, CGSizeMake(-2, -2), 4.0f, _shadowColor.CGColor);
  57.         }
  58.         else if (i+1!=_depth){
  59.             //add glow like blur
  60.             CGContextSetShadowWithColor(context, CGSizeMake(-1, -1), 3.0f, _newColor.CGColor);
  61.         }
  62.         [_text drawAtPoint:CGPointMake(i, i) withFont:_font];
  63.         CGContextRestoreGState(context);        
  64.     }
  65.     UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
  66.     UIGraphicsEndImageContext();
  67.     return finalImage;
  68. }
  69. @end
 
匯入category擴充然後如下使用: 
 
複製代碼
  1. 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];
  2. UIImageView *imgView = [[UIImageView alloc] initWithImage:my3dImage];
  3. [self.view addSubview: imgView];

如何讓IOS中的文本實現3D效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.