日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何让IOS中的文本实现3D效果

發(fā)布時間:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何让IOS中的文本实现3D效果 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

?
?

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

深白色 級別:?小白 發(fā)帖
23
云幣
45
  • 加關(guān)注
  • 寫私信
只看該作者沙發(fā)??發(fā)表于: 2014-06-10 Re如何讓IOS中的文本實現(xiàn)3D效果 提供一個方法,不斷的重復(fù)畫文本的layer,創(chuàng)建有層次的效果:?
創(chuàng)建UIImage Category,命名為UIImage+3d.h文件: 復(fù)制代碼
  • //
  • //??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文件:?
    復(fù)制代碼
  • //
  • //??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;
  • }
  • @end
  • ?
    導(dǎo)入category擴展然后如下使用:?
    ?
    復(fù)制代碼
  • 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];
  • 總結(jié)

    以上是生活随笔為你收集整理的如何让IOS中的文本实现3D效果的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。