Swift: 如何从 ViewDidLoad 获取 UITextView 上的阴影?
Swift: How to get shadow working on UITextView from ViewDidLoad?
我无法在 UIImageView
内的 UITextView
上获得阴影。
我尝试了很多可能的方法。限制是,我不能覆盖 layoutMethods
因为它们出于某种原因无法访问。因此,为了演示,我将相同的代码放在 viewDidLoad 中进行调试,但无法弄清楚。请在这里解释我做错了什么。
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
let imageView = UIImageView()
imageView.image = #imageLiteral(resourceName: "guitar")
imageView.layer.cornerRadius = 15
imageView.clipsToBounds = true
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.heightAnchor.constraint(equalToConstant: 240).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 240).isActive = true
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
let textView = UITextView(frame: CGRect.zero)
textView.text = "This is a Sample text. This is a Sample text. This is a Sample text. This is a Sample text. This is a Sample text. "
textView.isEditable = false
textView.textAlignment = .left
textView.isSelectable = false
textView.backgroundColor = .systemBlue
textView.textColor = UIColor.white
textView.sizeToFit()
textView.isScrollEnabled = false
textView.textContainerInset = UIEdgeInsets(top: 6, left: 7, bottom: 6, right: 7)
textView.clipsToBounds = false
textView.layer.shadowColor = UIColor.gray.cgColor
textView.layer.shadowRadius = 10
textView.layer.opacity = 1
textView.layer.shadowOffset = .zero
textView.layer.shadowPath = UIBezierPath(rect: textView.bounds).cgPath
textView.layer.masksToBounds = false
imageView.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.leftAnchor.constraint(equalTo: imageView.leftAnchor).isActive = true
textView.bottomAnchor.constraint(equalTo: imageView.bottomAnchor).isActive = true
textView.rightAnchor.constraint(equalTo: imageView.rightAnchor).isActive = true
}
您将 LAYER 不透明度设置为 1
:
textView.layer.opacity = 1
但是你需要设置图层的shadowOpacity:
//textView.layer.opacity = 1
textView.layer.shadowOpacity = 1
这应该可以解决问题。
顺便说一句,无论是否使用 .shadowPath
,您都会得到相同的结果(因为 "default" 阴影路径是视图的边界/矩形)。
我无法在 UIImageView
内的 UITextView
上获得阴影。
我尝试了很多可能的方法。限制是,我不能覆盖 layoutMethods
因为它们出于某种原因无法访问。因此,为了演示,我将相同的代码放在 viewDidLoad 中进行调试,但无法弄清楚。请在这里解释我做错了什么。
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
let imageView = UIImageView()
imageView.image = #imageLiteral(resourceName: "guitar")
imageView.layer.cornerRadius = 15
imageView.clipsToBounds = true
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.heightAnchor.constraint(equalToConstant: 240).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 240).isActive = true
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
let textView = UITextView(frame: CGRect.zero)
textView.text = "This is a Sample text. This is a Sample text. This is a Sample text. This is a Sample text. This is a Sample text. "
textView.isEditable = false
textView.textAlignment = .left
textView.isSelectable = false
textView.backgroundColor = .systemBlue
textView.textColor = UIColor.white
textView.sizeToFit()
textView.isScrollEnabled = false
textView.textContainerInset = UIEdgeInsets(top: 6, left: 7, bottom: 6, right: 7)
textView.clipsToBounds = false
textView.layer.shadowColor = UIColor.gray.cgColor
textView.layer.shadowRadius = 10
textView.layer.opacity = 1
textView.layer.shadowOffset = .zero
textView.layer.shadowPath = UIBezierPath(rect: textView.bounds).cgPath
textView.layer.masksToBounds = false
imageView.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.leftAnchor.constraint(equalTo: imageView.leftAnchor).isActive = true
textView.bottomAnchor.constraint(equalTo: imageView.bottomAnchor).isActive = true
textView.rightAnchor.constraint(equalTo: imageView.rightAnchor).isActive = true
}
您将 LAYER 不透明度设置为 1
:
textView.layer.opacity = 1
但是你需要设置图层的shadowOpacity:
//textView.layer.opacity = 1
textView.layer.shadowOpacity = 1
这应该可以解决问题。
顺便说一句,无论是否使用 .shadowPath
,您都会得到相同的结果(因为 "default" 阴影路径是视图的边界/矩形)。