Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // UIExpanderLabelv2.swift
- // TestingMore
- //
- // Created by Richard Cumming on 21/07/2019.
- // Copyright © 2019 dracosveen's Studio. All rights reserved.
- //
- import UIKit
- class UIExpanderLabelv2: UILabel {
- // MARK: fields
- private var button = UIButton(frame: CGRect(x: 0, y: 0, width: 65, height: 15))
- private var isExpanded = false
- private var internalLabel: UILabel?
- @IBInspectable var topInset: CGFloat = 5.0
- @IBInspectable var bottomInset: CGFloat = 5.0
- @IBInspectable var leftInset: CGFloat = 7.0
- @IBInspectable var rightInset: CGFloat = 7.0
- override func drawText(in rect: CGRect) {
- let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
- super.drawText(in: rect.inset(by: insets))
- }
- override var intrinsicContentSize: CGSize {
- let size = super.intrinsicContentSize
- return CGSize(width: size.width + leftInset + rightInset,
- height: size.height + topInset + bottomInset)
- }
- override func didMoveToSuperview() {
- setupView()
- }
- // MARK: inti
- override init(frame: CGRect) {
- super.init(frame: frame)
- }
- required init?(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- }
- //MARK: private func
- private func setupView() {
- layer.cornerRadius = 5
- layer.masksToBounds = true
- numberOfLines = 3
- lineBreakMode = .byWordWrapping
- sizeToFit()
- internalLabel = self
- self.superview!.addSubview(button)
- setupButton()
- }
- private func setupButton() {
- button.addTarget(self, action: #selector(buttonTouched(sender:)), for: .touchUpInside)
- setButtonText(text: "more")
- button.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- button.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor, constant: 4),
- button.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor, constant: 5),
- button.heightAnchor.constraint(equalToConstant: 15),
- button.widthAnchor.constraint(equalToConstant: 65)
- ])
- // gradient
- let gradientLayer = CAGradientLayer()
- gradientLayer.frame = button.frame
- gradientLayer.colors = [internalLabel!.backgroundColor!.withAlphaComponent(0).cgColor, internalLabel!.backgroundColor!.withAlphaComponent(1).cgColor]
- gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
- gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)
- //Use diffrent colors
- button.layer.addSublayer(gradientLayer)
- }
- @objc private func buttonTouched(sender: UIButton) {
- if isExpanded {
- isExpanded = false
- setButtonText(text: "more")
- numberOfLines = 3
- lineBreakMode = .byTruncatingTail
- sizeToFit()
- }
- else {
- numberOfLines = 0
- sizeToFit()
- // translatesAutoresizingMaskIntoConstraints = false
- // NSLayoutConstraint.activate([
- // heightAnchor.constraint(equalToConstant: frame.height + 25)
- // ])
- isExpanded = true
- setButtonText(text: "less")
- }
- }
- private func setButtonText(text: String) {
- let attributedString = NSAttributedString(
- string: NSLocalizedString(text, comment: ""),
- attributes:[
- NSAttributedString.Key.font :UIFont.systemFont(ofSize: 13.0),
- NSAttributedString.Key.foregroundColor : UIColor.blue,
- ])
- button.setAttributedTitle(attributedString, for: .normal)
- button.contentHorizontalAlignment = .right
- button.contentVerticalAlignment = .center
- }
- }
- //
- // ViewController.swift
- // TestingMore
- //
- // Created by Richard Cumming on 15/07/2019.
- // Copyright © 2019 dracosveen's Studio. All rights reserved.
- //
- import UIKit
- class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- }
- override func viewWillAppear(_ animated: Bool) {
- }
- override func viewDidAppear(_ animated: Bool) {
- let margins = view.layoutMarginsGuide
- let myView = UIExpanderLabelv2()
- myView.backgroundColor = .white
- view.addSubview(myView)
- myView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- myView.topAnchor.constraint(equalTo: margins.topAnchor, constant: 0),
- myView.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 0),
- myView.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 0)])
- myView.font = UIFont.systemFont(ofSize: 12)
- myView.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement