iPhone UILabel text soft shadow

前端 未结 15 922
时光取名叫无心
时光取名叫无心 2020-12-07 09:31

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

EDIT:

相关标签:
15条回答
  • 2020-12-07 10:15

    Apply the (soft) shadow on the view's layer, like this:

    UILabel *label = [[UIabel alloc] init];
    label.layer.shadowColor = [[UIColor whiteColor] CGColor];
    label.layer.shadowOpacity = 1.0;
    
    0 讨论(0)
  • 2020-12-07 10:15

    I tried almost all of these techniques (except FXLabel) and couldn't get any of them to work with iOS 7. I did eventually find THLabel which is working perfectly for me. I used THLabel in Interface Builder and setup User Defined Runtime Attributes so that it's easy for a non programmer to control the look and feel.

    https://github.com/MuscleRumble/THLabel

    0 讨论(0)
  • 2020-12-07 10:16

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

    https://github.com/nicklockwood/FXLabel

    0 讨论(0)
  • 2020-12-07 10:16

    As of iOS 5 Apple provides a private api method to create labels with soft shadows. The labels are very fast: I'm using dozens at the same time in a series of transparent views and there is no slowdown in scrolling animation.

    This is only useful for non-App Store apps (obviously) and you need the 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];
    
    0 讨论(0)
  • 2020-12-07 10:20

    While it's impossible to set a blur radius directly on UILabel, you definitely could change it by manipulating CALayer.

    Just set:

    //Required properties
    customLabel.layer.shadowRadius = 5.0 //set shadow radius to your desired value.
    customLabel.layer.shadowOpacity = 1.0 //Choose an opacity. Make sure it's visible (default is 0.0)
    
    //Other options
    customLabel.layer.shadowOffset = CGSize(width: 10, height: 10)
    customLabel.layer.shadowColor = UIColor.black.cgColor
    customLabel.layer.masksToBounds = false
    

    What I hope will help someone and other answers failed to clarify is that it will not work if you also set UILabel Shadow Color property directly on Interface Builder while trying to setup .layer.shadowRadius.

    So if setting label.layer.shadowRadius didn't work, please verify Shadow Color for this UILabel on Interface Builder. It should be set to default. And then, please, if you want a shadow color other than black, set this color also through .layer property.

    enter image description here

    0 讨论(0)
  • 2020-12-07 10:22

    This 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;
    
    0 讨论(0)
提交回复
热议问题