IOS - распространение тени на 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
Ответы
Ответ 1
Кажется, это работает для меня на детской площадке. Вам просто нужно увеличить радиус тени, чтобы он выглядел так, как вы этого хотите.
Это точный код, который я использовал
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)
Или, если вы используете его на фоне размытия, вы можете использовать вибрацию для достижения лучшего эффекта.
![введите описание изображения здесь]()
Ответ 2
//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 = 5
shadow.shadowOffset = CGSize(width: 0, height: 0)
shadow.shadowColor = UIColor.black.withAlphaComponent(0.3)
let attributes = [ NSAttributedString.Key.shadow: shadow ]
let attributedString = NSAttributedString(string: string, attributes: attributes)
self.attributedText = attributedString
}
}
Ответ 3
Рикола ответит как Свифт 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)
на правой панели для визуального представления метки.