iPhone UILabel text soft shadow

前端 未结 15 921
时光取名叫无心
时光取名叫无心 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:07

    As of 3.2 there is direct support for shadows 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 your shadow clipped by the label bounds:

    label.layer.masksToBounds = NO;
    

    finally set

    label.layer.shouldRasterize = YES;
    
    0 讨论(0)
  • 2020-12-07 10:08

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

    UILabel* label = [[UILabel alloc] init];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake(0,1);
    
    0 讨论(0)
  • 2020-12-07 10:10

    Subclass UILabel, as stated, then, in drawRect:, do [self drawTextInRect:rect]; to get the text drawn into the current context. Once it is in there, you can start working with it by adding filters and whatnot. If you want to make a drop shadow with what you just drew into the context, you should be able to use:

    CGContextSetShadowWithColor()
    

    Look that function up in the docs to learn how to use it.

    0 讨论(0)
  • 2020-12-07 10:12
    _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 the [UIColor colorWithWhite:0 alpha:0.2] to set the alpha value.

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

    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 via:

    label.shadow()
    
    0 讨论(0)
  • 2020-12-07 10:14

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

    0 讨论(0)
提交回复
热议问题