2016-01-31 33 views
8

我想增加UILabel中的影子传播,但我无法找到要执行此操作的属性。我在下面添加了一张照片来说明问题。iOS - UILabel上的影子传播

An example of the desired label shadow, and the current one.

顶部标签是我能够在Photoshop中创建了预期的效果。底部的标签说明了我在iOS中能够找到的属性。这是我用于底部标签的代码。

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 

我没有找到一个建议,使用第二个标签的阴影,但是这并没有得到预期的结果。这是该标签的代码。

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 
+0

其实,有关于阴影的好文章https://makeapppie.com/2015/05/19/swift-swift-how-to-make-a-drop-shadow-in-user-interfaces/ –

回答

22

它似乎在我的操场上工作。你只需要增加阴影半径以使它看起来像你想要的那样。

这是确切的操场代码我用

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) 

或者,如果你使用的是它在模糊观察背景,你可以用活力来达到更好的效果看。

enter image description here

+0

达尼特,你打败了我。大声笑。试图找出如何使用游乐场后不使用它很长时间... – DeveloperACE

+0

谢谢你的快速反应!我看着它,但它不是我正在寻找的结果。我更新了图像以更好地匹配您的Playground示例。 –

+0

非常感谢! – CaffeineShots

1

Rikola的答案斯威夫特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) 

按小“眼睛”用吸尘器吸尘过在右侧栏中的view.addSubview(bottomLabel)打印时,对于标签的可视化表示。

4
//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