Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.62 KB | None | 0 0
  1. //
  2. //  UIExpanderLabelv2.swift
  3. //  TestingMore
  4. //
  5. //  Created by Richard Cumming on 21/07/2019.
  6. //  Copyright © 2019 dracosveen's Studio. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class UIExpanderLabelv2: UILabel {
  12.    
  13.     // MARK: fields
  14.     private var button = UIButton(frame: CGRect(x: 0, y: 0, width: 65, height: 15))
  15.     private var isExpanded = false
  16.     private var internalLabel: UILabel?
  17.    
  18.     @IBInspectable var topInset: CGFloat = 5.0
  19.     @IBInspectable var bottomInset: CGFloat = 5.0
  20.     @IBInspectable var leftInset: CGFloat = 7.0
  21.     @IBInspectable var rightInset: CGFloat = 7.0
  22.    
  23.     override func drawText(in rect: CGRect) {
  24.         let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
  25.         super.drawText(in: rect.inset(by: insets))
  26.     }
  27.    
  28.     override var intrinsicContentSize: CGSize {
  29.         let size = super.intrinsicContentSize
  30.         return CGSize(width: size.width + leftInset + rightInset,
  31.                       height: size.height + topInset + bottomInset)
  32.     }
  33.    
  34.     override func didMoveToSuperview() {
  35.         setupView()
  36.     }
  37.  
  38.     // MARK: inti
  39.     override init(frame: CGRect) {
  40.         super.init(frame: frame)
  41.        
  42.     }
  43.    
  44.     required init?(coder aDecoder: NSCoder) {
  45.         super.init(coder: aDecoder)
  46.     }
  47.    
  48.     //MARK: private func
  49.     private func setupView() {
  50.         layer.cornerRadius = 5
  51.         layer.masksToBounds = true
  52.        
  53.         numberOfLines = 3
  54.         lineBreakMode = .byWordWrapping
  55.         sizeToFit()
  56.        
  57.         internalLabel = self
  58.         self.superview!.addSubview(button)
  59.         setupButton()
  60.        
  61.     }
  62.    
  63.     private func setupButton() {
  64.         button.addTarget(self, action: #selector(buttonTouched(sender:)), for: .touchUpInside)
  65.         setButtonText(text: "more")
  66.        
  67.         button.translatesAutoresizingMaskIntoConstraints = false
  68.         NSLayoutConstraint.activate([
  69.             button.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor, constant: 4),
  70.             button.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor, constant: 5),
  71.             button.heightAnchor.constraint(equalToConstant: 15),
  72.             button.widthAnchor.constraint(equalToConstant: 65)
  73.             ])
  74.        
  75.         // gradient
  76.         let gradientLayer = CAGradientLayer()
  77.         gradientLayer.frame = button.frame
  78.         gradientLayer.colors = [internalLabel!.backgroundColor!.withAlphaComponent(0).cgColor, internalLabel!.backgroundColor!.withAlphaComponent(1).cgColor]
  79.         gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
  80.         gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)
  81.        
  82.         //Use diffrent colors
  83.         button.layer.addSublayer(gradientLayer)
  84.        
  85.     }
  86.    
  87.     @objc private func buttonTouched(sender: UIButton) {
  88.         if isExpanded {
  89.             isExpanded = false
  90.             setButtonText(text: "more")
  91.             numberOfLines = 3
  92.             lineBreakMode = .byTruncatingTail
  93.             sizeToFit()
  94.         }
  95.         else {
  96.             numberOfLines = 0
  97.             sizeToFit()
  98.            
  99. //            translatesAutoresizingMaskIntoConstraints = false
  100. //            NSLayoutConstraint.activate([
  101. //                heightAnchor.constraint(equalToConstant: frame.height + 25)
  102. //                ])
  103.            
  104.             isExpanded = true
  105.             setButtonText(text: "less")
  106.         }
  107.        
  108.     }
  109.    
  110.     private func setButtonText(text: String) {
  111.        
  112.         let attributedString = NSAttributedString(
  113.             string: NSLocalizedString(text, comment: ""),
  114.             attributes:[
  115.                 NSAttributedString.Key.font :UIFont.systemFont(ofSize: 13.0),
  116.                 NSAttributedString.Key.foregroundColor : UIColor.blue,
  117.             ])
  118.         button.setAttributedTitle(attributedString, for: .normal)
  119.         button.contentHorizontalAlignment = .right
  120.         button.contentVerticalAlignment = .center
  121.        
  122.     }
  123. }
  124.  
  125. //
  126. //  ViewController.swift
  127. //  TestingMore
  128. //
  129. //  Created by Richard Cumming on 15/07/2019.
  130. //  Copyright © 2019 dracosveen's Studio. All rights reserved.
  131. //
  132.  
  133. import UIKit
  134.  
  135. class ViewController: UIViewController {
  136.    
  137.     override func viewDidLoad() {
  138.         super.viewDidLoad()
  139.         // Do any additional setup after loading the view.
  140.        
  141.     }
  142.    
  143.     override func viewWillAppear(_ animated: Bool) {
  144.        
  145.        
  146.     }
  147.    
  148.     override func viewDidAppear(_ animated: Bool) {
  149.         let margins = view.layoutMarginsGuide
  150.        
  151.         let myView = UIExpanderLabelv2()
  152.         myView.backgroundColor = .white
  153.         view.addSubview(myView)
  154.        
  155.        
  156.        
  157.         myView.translatesAutoresizingMaskIntoConstraints = false
  158.         NSLayoutConstraint.activate([
  159.             myView.topAnchor.constraint(equalTo: margins.topAnchor, constant: 0),
  160.             myView.leadingAnchor.constraint(equalTo: margins.leadingAnchor, constant: 0),
  161.             myView.trailingAnchor.constraint(equalTo: margins.trailingAnchor, constant: 0)])
  162.        
  163.         myView.font = UIFont.systemFont(ofSize: 12)
  164.        
  165.         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."
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement