iPhone UILabel text soft shadow

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

    To keep things up to date: Creating the shadow in Swift is as easy as that:

    Import the QuartzCore Framework

    import QuartzCore
    

    And set the shadow attributes to your label

    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
    
    0 讨论(0)
  • 2020-12-07 10:24

    Additionally to IIDan's answer: For some purposes it is necessary to set

    label.layer.shouldRasterize = YES
    

    I think this is due to the blend mode that is used to render the shadow. For example I had a dark background and white text on it and wanted to "highlight" the text using a black shadowy glow. It wasn't working until I set this property.

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

    Subclass UILabel, and override -drawInRect:

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