在Swift中的UITextView上投影

时间:2018-08-15 11:41:33

标签: ios swift uiview uitextview shadow

我已经搜索了一段时间,但无法弄清楚如何实施找到的解决方案。

在下面的文章中定义了我真正想要的东西,并且在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
}

Screenshot

图片参考:UITextView bottom shadow on text帖子中的@ user2213271

这是我尝试过的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
}

1 个答案:

答案 0 :(得分:0)

似乎您忘记了在设置kvkkTextView.translatesAutoresizingMaskIntoConstraints = false的锚之前添加kvkkTextView。因此,您的kvkkTextView无法从约束中获得框架。