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
- //
- Uiimage+3d.h
- //
- Created by Lefteris Haritou on 12/10/12.
- Feel free to use this code, 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 file: Copy Code
- //
- Uiimage+3d.m
- //
- Created by Lefteris Haritou on 12/10/12.
- Feel free to use this code, 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'll need for our text
- Cgsize expectedsize = [_text sizewithfont:_font constrainedtosize:cgsizemake (Maxfloat, MAXFLOAT)];
- Increase our size, as we'll 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 is going to slightly decrease the color as we move back
- So here we is going to create a color array that we'll 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 is inserting always at first index as we want this array of colors to is reversed (darkest color being the last)
- [_colorsarray Insertobject:_newcolor atindex:0];
- }
- We'll 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 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 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
Import the category extension and use it as follows:
Copy Code
- 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];
|