How to make text in iOS achieve 3D effects

Source: Internet
Author: User

This reprint to Http://bbs.aliyun.com/read/181991.html?spm=5176.7114037.1996646101.25.p0So7c&pos=9

Zhedianshi level: Help Group
Post
487
Cloud Currency
430
  • Add attention
  • Write private messages
only see the landlord more operators posted: 2014-06-10
I want to render some text in iOS in 3D, using the Uikit and Standard view controllers.
The effect of the implementation is likely to be this:


Can it be implemented via iOS and Uikit? I only use a static PNG image, the text content changes according to the user data.
Key words: iOS development 3D effect phase 46th: Beijing Cloud Habitat Conference, nail nail, fast black hole, code farming growth diary Share to the Tao Lake Sina QQ microblogging QQ space Happy everyone douban easy Weibo Baidu Fresh Fruit White Society fetion reply citation Report

Dark White level: small white
Post
23
Cloud Currency
45
  • Add attention
  • Write private messages
just look at the author sofa Posted in: 2014-06-10 How re enables the 3D effect of text in iOS to provide a way to constantly repeat a layer of text and create layered effects:
Create UIImage Category, named uiimage+3d.h File: Copy code
    1. //
    2. Uiimage+3d.h
    3. //
    4. Created by Lefteris Haritou on 12/10/12.
    5. Feel free to use this code, 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 file:
Copy Code
  1. //
  2. Uiimage+3d.m
  3. //
  4. Created by Lefteris Haritou on 12/10/12.
  5. Feel free to use this code, 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'll need for our text
  11. Cgsize expectedsize = [_text sizewithfont:_font constrainedtosize:cgsizemake (Maxfloat, MAXFLOAT)];
  12. Increase our size, as we'll 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 is going to slightly decrease the color as we move back
  19. So here we is going to create a color array that we'll 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 is inserting always at first index as we want this array of colors to is reversed (darkest color being the last)
  34. [_colorsarray Insertobject:_newcolor atindex:0];
  35. }
  36. We'll 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 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 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

Import the category extension and use it as follows:

Copy Code
    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];

How to make text in iOS achieve 3D effects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.