iPhone UILabel text soft shadow

前端 未结 15 973
时光取名叫无心
时光取名叫无心 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条回答
  •  猫巷女王i
    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

提交回复
热议问题