[英]iOS - Shadow spread on UILabel
我想在我的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
我確實找到了使用第二個標簽作為陰影的建議,但這沒有給出所需的結果。 這是該標簽的代碼。
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
它似乎在操場上對我有用。 您只需增加陰影半徑即可使其顯示為您想要的效果。
這是我使用的確切游樂場代碼
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)
或者,如果您在模糊視圖背景上使用它,您可以使用活力來獲得更好看的效果。
//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
作為擴展:
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作為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)
當在view.addSubview(bottomLabel)
時,按下小“眼睛”,以便直觀地顯示標簽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.