eve*_*erk 12 shadow uilabel ios swift
我想在我的UILabel中增加阴影的传播,但我找不到属性来做到这一点.我在下面添加了一张照片来说明问题.
顶部标签是我能够在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
Run Code Online (Sandbox Code Playgroud)
我确实找到了使用第二个标签作为阴影的建议,但这没有给出所需的结果.这是该标签的代码.
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
Run Code Online (Sandbox Code Playgroud)
rik*_*ola 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)
Run Code Online (Sandbox Code Playgroud)
或者,如果您在模糊视图背景上使用它,您可以使用活力来获得更好看的效果.
小智 7
//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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11081 次 |
| 最近记录: |