UILabel soft shadow text for iPhone

I know that soft shadows are not supported by UILabel out of the box on iPhone. So what would be the best way to implement my own?

EDIT:

Obviously, I am a subclass of UILabel and draw in -drawRect: My question is how to get the contents of the label as graphics and draw around them, blur them, etc ...

EDIT 2:

I returned to this question about a year later. In the meantime, I created a class that makes it easy to add a soft shadow to the label and adjust its radius, etc., and also draw gradients on the text itself. You can find it on GitHub: https://github.com/doukasd/iOS-Components/tree/master/Views

+55
objective-c iphone uilabel shadow
Nov 10 '09 at 16:13
source share
14 answers

This answer to this similar question provides code for drawing a blurry shadow behind a UILabel. The author uses CGContextSetShadow () to generate a shadow for the drawn text.

+18
Nov 10 '09 at 18:19
source share

Starting with 3.2, there is direct shadow support in the SDK.

 label.layer.shadowColor = [label.textColor CGColor]; label.layer.shadowOffset = CGSizeMake(0.0, 0.0); 

Import <QuartzCore/QuartzCore.h> and play with some parameters:

 label.layer.shadowRadius = 3.0; label.layer.shadowOpacity = 0.5; 

And, if you find a shadow limited by the borders of the labels:

 label.layer.masksToBounds = NO; 

finally install

 label.layer.shouldRasterize = YES 
+184
Jan 20 2018-11-11T00:
source share

I advise you to use the shadowColor and shadowOffset properties for UILabel:

 UILabel* label = [[UILabel alloc] init]; label.shadowColor = [UIColor whiteColor]; label.shadowOffset = CGSizeMake(0,1); 
+37
Mar 02 '10 at 13:15
source share

In addition to the IIDan request: For some purposes, you must install

 label.layer.shouldRasterize = YES 

I think this is due to the blend mode, which is used to render the shadow. For example, I had a dark background and white text, and he wanted to "highlight" the text with a black shadow glow. It did not work until I set this property.

+15
Mar 20 '13 at 16:31
source share

Apply a (soft) shadow at the view level, for example:

 UILabel *label = [[UIabel alloc] init]; label.layer.shadowColor = [[UIColor whiteColor] CGColor]; label.layer.shadowOpacity = 1.0; 
+6
May 19 '10 at 9:45 a.m.
source share

To keep it up-to-date: creating a shadow in Swift is just as easy:

Import the QuartzCore platform

 import QuartzCore 

And set the shadow attributes to your shortcut

 titleLabel.shadowColor = UIColor.blackColor() titleLabel.shadowOffset = CGSizeMake(0.0, 0.0) titleLabel.layer.shadowRadius = 5.0 titleLabel.layer.shadowOpacity = 0.8 titleLabel.layer.masksToBounds = false titleLabel.layer.shouldRasterize = true 
+5
Aug 19 '15 at 11:14
source share
 _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _nameLabel.font = [UIFont boldSystemFontOfSize:19.0f]; _nameLabel.textColor = [UIColor whiteColor]; _nameLabel.backgroundColor = [UIColor clearColor]; _nameLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.2]; _nameLabel.shadowOffset = CGSizeMake(0, 1); 

I think you should use [UIColor colorWithWhite: 0 alpha: 0.2] to set the alpha value.

+4
Jul 22 '13 at 3:17
source share

I tried almost all of these methods (except FXLabel) and could not get them to work with iOS 7. In the end, I found THLabel, which works fine for me. I used THLabel in the Builder interface and set up custom runtime attributes so that no programmer could control the look.

https://github.com/MuscleRumble/THLabel

+4
Nov 22 '13 at 16:03
source share

It's like a trick

 UILabel *customLabel = [[UILabel alloc] init]; UIColor *color = [UIColor blueColor]; customLabel.layer.shadowColor = [color CGColor]; customLabel.layer.shadowRadius = 5.0f; customLabel.layer.shadowOpacity = 1; customLabel.layer.shadowOffset = CGSizeZero; customLabel.layer.masksToBounds = NO; 
+4
Jan 09 '15 at 7:39
source share

A subclass of UILabel, as indicated, then, in drawRect :, do [self drawTextInRect:rect]; to get the text pulled into the current context. When he is there, you can start working with him by adding filters and much more. If you want to make a shadow with what you just used in context, you should be able to use:

 CGContextSetShadowWithColor() 

See what the function in the docs knows how to use it.

+3
Nov 10 '09 at 18:20
source share

I wrote a library that provides a subclass of UILabel with soft shadow support and a bunch of other effects:

https://github.com/nicklockwood/FXLabel

+3
Jun 07 '12 at 9:20
source share

Starting with iOS 5, Apple provides a private api method for creating labels with soft shadows. Shortcuts are very fast: I use dozens at the same time in a series of transparent views and there is no slowdown in the animation scroll.

This is only useful for non-app store apps (obviously), and you need a header file.

 $SBBulletinBlurredShadowLabel = NSClassFromString("SBBulletinBlurredShadowLabel"); CGRect frame = CGRectZero; SBBulletinBlurredShadowLabel *label = [[[$SBBulletinBlurredShadowLabel alloc] initWithFrame:frame] autorelease]; label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor whiteColor]; label.font = [UIFont boldSystemFontOfSize:12]; label.text = @"I am a label with a soft shadow!"; [label sizeToFit]; 
+1
Oct 05 2018-11-22T00:
source share

In Swift 3 you can create an extension:

 import UIKit extension UILabel { func shadow() { self.layer.shadowColor = self.textColor.cgColor self.layer.shadowOffset = CGSize.zero self.layer.shadowRadius = 3.0 self.layer.shadowOpacity = 0.5 self.layer.masksToBounds = false self.layer.shouldRasterize = true } } 

and use it through:

 label.shadow() 
+1
Jan 05 '17 at 8:00
source share

Subclass UILabel and override -drawInRect:

-3
Nov 10 '09 at 16:15
source share



All Articles