在 Swift 中的 UITextView 上放置阴影
Posted
技术标签:
【中文标题】在 Swift 中的 UITextView 上放置阴影【英文标题】:Drop shadow on UITextView in Swift 【发布时间】:2018-08-15 11:41:33 【问题描述】:我搜索了一段时间,但不知道如何实施我找到的解决方案。
下面的帖子定义了我真正想要的东西,它也有一个解决方案,但在 Objective-c 中。我尝试实施该解决方案,但实施后文本开始不显示。
UITextView bottom shadow on text
class AggrementView: UIView, UITextViewDelegate
let acceptBtn = RippleButton()
let kvkkTextView = UITextView()
private let containerView = UIView()
override init(frame: CGRect)
super.init(frame: frame)
setupUI()
setupConstraints()
public convenience init(text: String, imageName: String)
self.init()
public convenience init(text: String, imageName: String, senderColor: UIColor)
self.init()
required public init?(coder aDecoder: NSCoder)
fatalError("init(coder:) has not been implemented")
private func setupUI()
self.backgroundColor = .clear
acceptBtn.translatesAutoresizingMaskIntoConstraints = false
//acceptBtn.backgroundColor = UIColor(red:0.40, green:0.69, blue:0.07, alpha:1.0)
acceptBtn.backgroundColor = UIColor.gray
acceptBtn.layer.cornerRadius = 6.0
acceptBtn.setTitle("Onaylıyorum", for: UIControlState.normal)
acceptBtn.titleLabel?.font = UIFont(name: "Corbert-Regular", size: 20)!
acceptBtn.setTitleColor(UIColor.flatWhite, for: UIControlState.normal)
//acceptBtn.setAllSideShadow(shadowShowSize: 8.0)
acceptBtn.accessibilityIdentifier = "acceptBtn"
acceptBtn.isEnabled = false
acceptBtn.setTitleColor(UIColor.lightGray, for: .disabled)
kvkkTextView.font = UIFont(name: "Corbert-Regular", size: 15)!
kvkkTextView.setContentOffset(CGPoint.zero, animated: false)
kvkkTextView.layer.cornerRadius = 6.0
kvkkTextView.backgroundColor = .clear
kvkkTextView.layer.borderColor = UIColor.flatGray.cgColor
kvkkTextView.textColor = UIColor.flatWhite
kvkkTextView.delegate = self
kvkkTextView.isEditable = false
containerView.backgroundColor = .clear
containerView.layer.cornerRadius = 6.0
self.addSubview(containerView)
containerView.addSubview(acceptBtn)
containerView.addSubview(kvkkTextView)
private func setupConstraints()
containerView.anchor(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 90, leftConstant: 10, bottomConstant: 20, rightConstant: 10, widthConstant: 0, heightConstant: 0)
kvkkTextView.anchor(self.containerView.topAnchor, left: self.containerView.leftAnchor, bottom: self.acceptBtn.topAnchor, right: self.containerView.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 20, rightConstant: 0, widthConstant: 0, heightConstant: 0)
acceptBtn.anchor(nil, left: self.containerView.leftAnchor, bottom: self.containerView.bottomAnchor, right: self.containerView.rightAnchor, topConstant: 0, leftConstant: 10, bottomConstant: 10, rightConstant: 10, widthConstant: 0, heightConstant: 60)
func scrollViewDidScroll(_ scrollView: UIScrollView)
if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height)
print( "View scrolled to the bottom" )
UIView.animate(withDuration: 0.5)
self.acceptBtn.backgroundColor = UIColor(red:0.40, green:0.69, blue:0.07, alpha:1.0)
self.acceptBtn.isEnabled = true
let color = UIColor.lightGray
guard
let verticalIndicator = scrollView.subviews.last as? UIImageView,
verticalIndicator.backgroundColor != color,
verticalIndicator.image?.renderingMode != .alwaysTemplate
else return
verticalIndicator.layer.masksToBounds = true
verticalIndicator.layer.cornerRadius = verticalIndicator.frame.width / 2
verticalIndicator.backgroundColor = color
verticalIndicator.image = verticalIndicator.image?.withRenderingMode(.alwaysTemplate)
verticalIndicator.tintColor = .clear
图片参考:@user2213271 在UITextView bottom shadow on text 帖子中
这是我试过的梯度代码:
func applyShadow()
var layerWidth: CGFloat = 8.0
var gl1 = CAGradientLayer()
var col1 = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0).cgColor
var col2 = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0).cgColor
gl1.colors = [col2, col1, col1, col2]
gl1.locations = [0.0, 0.0, 0.85, 1.0]
//underTextView - this is my UIView
gl1.frame = CGRect(x: 0, y: 0, width: underTextView.frame.width - layerWidth, height: underTextView.frame.height)
//layer for scrollIndicator - it must be visible always good
var gl2 = CAGradientLayer()
gl2.colors = [col1, col1]
gl2.locations = [0.0, 1.0]
gl2.frame = CGRect(x: underTextView.frame.width - layerWidth, y: 0, width: layerWidth, height: underTextView.frame.height)
//create main layer
var gl = CAGradientLayer()
gl.addSublayer(gl1)
gl.addSublayer(gl2)
//add to mask of UIView
underTextView.layer.mask = gl
【问题讨论】:
【参考方案1】:您似乎忘记在设置kvkkTextView
的锚点之前添加kvkkTextView.translatesAutoresizingMaskIntoConstraints = false
。所以你的kvkkTextView
无法从约束中获取它的框架。
【讨论】:
是的。感谢您的提醒。我添加了但没有任何改变。我猜这与cliptobounds有关。 您将kvkkTextView
的高度和宽度约束设置为 0。如果您没有执行“如果约束为 0,则不使用约束”之类的控制,这可能是原因。以上是关于在 Swift 中的 UITextView 上放置阴影的主要内容,如果未能解决你的问题,请参考以下文章
Swift 4:@IBOutlet UITextView 崩溃
在 Swift 中的 UITextView 中添加占位符文本?
UITableview swift中的UITextview高度
如何从 Swift 中的 UITextView 获取链接范围