简体   繁体   English

iOS - 影子在UILabel传播

[英]iOS - Shadow spread on UILabel

I want to increase the spread of a shadow in my UILabel but I can't find the property to do that. 我想在我的UILabel中增加阴影的传播,但我找不到属性来做到这一点。 I have added a photo below to illustrate the issue. 我在下面添加了一张照片来说明问题。

所需标签阴影的示例,以及当前标签阴影。

The top Label is the desired effect I am able to create in Photoshop. 顶部标签是我能够在Photoshop中创建的所需效果。 The bottom Label illustrates the properties I was able to find in iOS. 底部标签说明了我能够在iOS中找到的属性。 Here is the code I used for the bottom Label. 这是我用于底部标签的代码。

let bottomLabel = UILabel(frame: CGRectMake(0, 0, maxWidth, 18))
bottomLabel.backgroundColor = UIColor.clearColor()
bottomLabel.textColor = UIColor.whiteColor()
bottomLabel.font = UIFont.boldSystemFontOfSize(16)
bottomLabel.text = title
bottomLabel.textAlignment = .Center
bottomLabel.numberOfLines = 1
bottomLabel.layer.shadowOffset = CGSize(width: 0, height: 0)
bottomLabel.layer.shadowOpacity = 1
bottomLabel.layer.shadowRadius = 2

I did find a suggestion to use a second label as a shadow but this did not give the desired result. 我确实找到了使用第二个标签作为阴影的建议,但这没有给出所需的结果。 Here is the code for that Label. 这是该标签的代码。

let bottomLabelShadow = UILabel(frame: CGRectMake(0, 1, maxWidth, 18)) 
bottomLabelShadow.backgroundColor = UIColor.clearColor()
bottomLabelShadow.textColor = UIColor.blackColor()
bottomLabelShadow.font = UIFont.boldSystemFontOfSize(16)
bottomLabelShadow.text = title
bottomLabelShadow.textAlignment = .Center
bottomLabelShadow.numberOfLines = 1
bottomLabelShadow.layer.shadowOffset = CGSize(width: 0, height: 0)
bottomLabelShadow.layer.shadowOpacity = 1
bottomLabelShadow.layer.shadowRadius = 2

It seems to work for me in a playground. 它似乎在操场上对我有用。 You just need to increase the shadow radius to make it appear as you want it. 您只需增加阴影半径即可使其显示为您想要的效果。

This is the exact playground code I used 这是我使用的确切游乐场代码

let view = UIView(frame: CGRectMake(0,0,100,20))
view.backgroundColor = UIColor.yellowColor()

let bottomLabel = UILabel(frame: CGRectMake(0, 0, 100, 20))
bottomLabel.backgroundColor = UIColor.clearColor()
bottomLabel.textColor = UIColor.whiteColor()
bottomLabel.font = UIFont.boldSystemFontOfSize(18)
bottomLabel.text = "Testing"
bottomLabel.textAlignment = .Center
bottomLabel.numberOfLines = 1
bottomLabel.layer.shadowOffset = CGSize(width: 0, height: 0)
bottomLabel.layer.shadowOpacity = 1
bottomLabel.layer.shadowRadius = 6

view.addSubview(bottomLabel)

Or if you are using it over a blur view background, you could use vibrancy to achieve a better looking effect. 或者,如果您在模糊视图背景上使用它,您可以使用活力来获得更好看的效果。

在此输入图像描述

//Try This! Hope this helps!!

// Create a string
let myString = "Shadow"

// Create a shadow
let myShadow = NSShadow()
myShadow.shadowBlurRadius = 3
myShadow.shadowOffset = CGSize(width: 3, height: 3)
myShadow.shadowColor = UIColor.gray

// Create an attribute from the shadow
let myAttribute = [ NSShadowAttributeName: myShadow ]

// Add the attribute to the string
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute)

// set the attributed text on a label
myLabel.attributedText = myAttrString

As an extension: 作为扩展:

extension UILabel {

    func setTextWithShadow(_ string: String) {

        let shadow = NSShadow()
        shadow.shadowBlurRadius = 3
        shadow.shadowOffset = CGSize(width: 3, height: 3)
        shadow.shadowColor = UIColor.black.withAlphaComponent(0.5)

        let attributes = [ NSAttributedString.Key.shadow: shadow ]
        let attributedString = NSAttributedString(string: string, attributes: attributes)

        self.attributedText = attributedString
    }

}

Rikola's answer as Swift 3: Rikola作为Swift 3的答案:

import UIKit

let view = UIView(frame: CGRect.init(x: 0, y: 0, width: 100, height: 20))
view.backgroundColor = UIColor.yellow

let bottomLabel = UILabel(frame: CGRect.init(x: 0, y: 0, width: 100, height: 20))
bottomLabel.backgroundColor = UIColor.clear
bottomLabel.textColor = UIColor.white
bottomLabel.font = UIFont.boldSystemFont(ofSize: 18)
bottomLabel.text = "Testing"
bottomLabel.textAlignment = .center
bottomLabel.numberOfLines = 1
bottomLabel.layer.shadowOffset = CGSize(width: 0, height: 0)
bottomLabel.layer.shadowOpacity = 1
bottomLabel.layer.shadowRadius = 6

view.addSubview(bottomLabel)

Press the little "eye" when hoovering over the view.addSubview(bottomLabel) print in the right bar, for a visual representation of the label. 当在view.addSubview(bottomLabel)时,按下小“眼睛”,以便直观地显示标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM