使 UILabel 或 UITextView 的最后几行在 swift 中淡出。请检查所附图片
Make last few lines of UILabel or UITextView fade out in swift. Please check the attached image
如何控制成这样?
这是我所做的:
- 我创建了一个 UIView,它具有我选择的高度、textView 的尾随、前导和底部约束。这是我的 gradientView,我还用它来添加其他控件,例如 'Read more' 按钮。
- 在 viewDidLayoutSubviews() 中,我为 gradientView 创建了一个渐变层。 (请注意,您需要在创建图层之前删除该图层或确保它只添加一次,否则每次调用 viewDidLayoutSubviews 时您的渐变都会变暗)
- 当我的文本发生变化时,我通过检查
textView.contentSize.height > textView.frame.height
来确定是否应该显示 gradientView
这里有一个游乐场示例
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
var gradientView: UIView?
var gradient:CAGradientLayer?
override func loadView() {
let view = UIView()
view.backgroundColor = .gray
let textView = UITextView()
textView.frame = CGRect(x: 20, y: 20, width: 200, height: 300)
textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eu gravida ligula, vitae venenatis felis. Suspendisse volutpat posuere pretium. Pellentesque quis quam ac velit tincidunt egestas. Phasellus sed scelerisque augue, interdum luctus eros. Praesent non augue eu enim dignissim convallis. Nunc commodo eros quis quam euismod, a malesuada ipsum mattis. Sed sit amet ipsum in justo dictum rutrum a sit amet sem. Sed sit amet mi vel nulla ornare congue. Nam elit ante, aliquam id consequat ac, pretium vel augue. Vivamus hendrerit commodo lectus, vel feugiat mi tempus non. Donec porta, ipsum id porttitor sodales, tortor lectus porta lacus, quis blandit turpis enim at libero. Donec ante est, rutrum quis malesuada a, accumsan at tortor. Nam molestie commodo nulla non suscipit. Nullam pellentesque nunc quam, vitae tempus turpis sollicitudin id. Integer vel varius urna, eleifend eleifend diam."
textView.textColor = .black
view.addSubview(textView)
let gradientViewFrame = CGRect(x:textView.frame.origin.x, y: textView.frame.origin.y + textView.frame.height - 100, width: textView.frame.width, height: 100)
gradientView = UIView(frame:gradientViewFrame)
view.addSubview(gradientView!)
self.view = view
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
gradient?.removeFromSuperlayer()
gradient = gradientView?.gradientBackground(from:.white, to: UIColor.white.withAlphaComponent(0), direction: .bottomToTop)
}
}
enum GradientDirection {
case leftToRight
case rightToLeft
case topToBottom
case bottomToTop
}
extension UIView {
func gradientBackground(from color1: UIColor, to color2: UIColor, direction: GradientDirection) -> CAGradientLayer {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = [color1.cgColor, color2.cgColor]
switch direction {
case .leftToRight:
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
case .rightToLeft:
gradient.startPoint = CGPoint(x: 1.0, y: 0.5)
gradient.endPoint = CGPoint(x: 0.0, y: 0.5)
case .bottomToTop:
gradient.startPoint = CGPoint(x: 0.5, y: 1.0)
gradient.endPoint = CGPoint(x: 0.5, y: 0.0)
case .topToBottom:
gradient.startPoint = CGPoint(x: 0.5, y: 0.0)
gradient.endPoint = CGPoint(x: 0.5, y: 1.0)
default:
break
}
self.layer.insertSublayer(gradient, at: 0)
return gradient
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()
如何控制成这样?
这是我所做的:
- 我创建了一个 UIView,它具有我选择的高度、textView 的尾随、前导和底部约束。这是我的 gradientView,我还用它来添加其他控件,例如 'Read more' 按钮。
- 在 viewDidLayoutSubviews() 中,我为 gradientView 创建了一个渐变层。 (请注意,您需要在创建图层之前删除该图层或确保它只添加一次,否则每次调用 viewDidLayoutSubviews 时您的渐变都会变暗)
- 当我的文本发生变化时,我通过检查
textView.contentSize.height > textView.frame.height
来确定是否应该显示 gradientView
这里有一个游乐场示例
import UIKit
import PlaygroundSupport
class MyViewController : UIViewController {
var gradientView: UIView?
var gradient:CAGradientLayer?
override func loadView() {
let view = UIView()
view.backgroundColor = .gray
let textView = UITextView()
textView.frame = CGRect(x: 20, y: 20, width: 200, height: 300)
textView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse eu gravida ligula, vitae venenatis felis. Suspendisse volutpat posuere pretium. Pellentesque quis quam ac velit tincidunt egestas. Phasellus sed scelerisque augue, interdum luctus eros. Praesent non augue eu enim dignissim convallis. Nunc commodo eros quis quam euismod, a malesuada ipsum mattis. Sed sit amet ipsum in justo dictum rutrum a sit amet sem. Sed sit amet mi vel nulla ornare congue. Nam elit ante, aliquam id consequat ac, pretium vel augue. Vivamus hendrerit commodo lectus, vel feugiat mi tempus non. Donec porta, ipsum id porttitor sodales, tortor lectus porta lacus, quis blandit turpis enim at libero. Donec ante est, rutrum quis malesuada a, accumsan at tortor. Nam molestie commodo nulla non suscipit. Nullam pellentesque nunc quam, vitae tempus turpis sollicitudin id. Integer vel varius urna, eleifend eleifend diam."
textView.textColor = .black
view.addSubview(textView)
let gradientViewFrame = CGRect(x:textView.frame.origin.x, y: textView.frame.origin.y + textView.frame.height - 100, width: textView.frame.width, height: 100)
gradientView = UIView(frame:gradientViewFrame)
view.addSubview(gradientView!)
self.view = view
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
gradient?.removeFromSuperlayer()
gradient = gradientView?.gradientBackground(from:.white, to: UIColor.white.withAlphaComponent(0), direction: .bottomToTop)
}
}
enum GradientDirection {
case leftToRight
case rightToLeft
case topToBottom
case bottomToTop
}
extension UIView {
func gradientBackground(from color1: UIColor, to color2: UIColor, direction: GradientDirection) -> CAGradientLayer {
let gradient = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = [color1.cgColor, color2.cgColor]
switch direction {
case .leftToRight:
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
case .rightToLeft:
gradient.startPoint = CGPoint(x: 1.0, y: 0.5)
gradient.endPoint = CGPoint(x: 0.0, y: 0.5)
case .bottomToTop:
gradient.startPoint = CGPoint(x: 0.5, y: 1.0)
gradient.endPoint = CGPoint(x: 0.5, y: 0.0)
case .topToBottom:
gradient.startPoint = CGPoint(x: 0.5, y: 0.0)
gradient.endPoint = CGPoint(x: 0.5, y: 1.0)
default:
break
}
self.layer.insertSublayer(gradient, at: 0)
return gradient
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()