简体   繁体   English

在Swift中的UITextView上投影

[英]Drop shadow on UITextView in Swift

I was searching this for a time but can't figure out how to implement the solutions I found. 我搜索了一段时间,但无法弄清楚如何实施找到的解决方案。

Below post is defining what I really want and also It has a solution but in objective-c. 下面的帖子定义了我真正想要的东西,并且在Objective-C中也有解决方案。 I try to implement that solution but text started not to shown after that implementation. 我尝试实现该解决方案,但在该实现之后开始没有显示文本。

UITextView bottom shadow on text UITextView文本上的底部阴影

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
}

屏幕截图

Picture ref: @user2213271 in UITextView bottom shadow on text post 图片引用:@ user2213271在文本发布的UITextView底部阴影中

Here is the gradientCode I tried: 这是我尝试过的gradientCode:

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
}

It seems you forgot to add kvkkTextView.translatesAutoresizingMaskIntoConstraints = false before set kvkkTextView 's anchor. 似乎您忘记了在设置kvkkTextView的锚点之前添加kvkkTextView.translatesAutoresizingMaskIntoConstraints = false So your kvkkTextView can't get its frame from constraints. 因此,您的kvkkTextView无法从约束中获取其框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM