gpt4 book ai didi

ios - 在 Swift 中的 UITextView 上放置阴影

转载 作者:行者123 更新时间:2023-11-28 07:42:35 34 4
gpt4 key购买 nike

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

下面的帖子定义了我真正想要的东西,它也有一个解决方案,但在 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

图片引用:@user2213271 UITextView bottom shadow on text发布

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

最佳答案

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

关于ios - 在 Swift 中的 UITextView 上放置阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51858023/

34 4 0
文章推荐: javascript - 注入(inject)由 module.provider 定义的服务
文章推荐: jquery - 在应用程序的 .erb View 中交替使用 rails image_uploader.rb 生成
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com