Advertisement
Don_Mag

UILabel and UITextView truncation issues

Jan 12th, 2023
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.63 KB | None | 0 0
  1. // a replacement for UILabel
  2. class TextViewLabel: UITextView {
  3.    
  4.     public var numberOfLines: Int = 0 {
  5.         didSet {
  6.             textContainer.maximumNumberOfLines = numberOfLines
  7.         }
  8.     }
  9.     public var lineBreakMode: NSLineBreakMode = .byTruncatingTail {
  10.         didSet {
  11.             textContainer.lineBreakMode = lineBreakMode
  12.         }
  13.     }
  14.    
  15.     override init(frame: CGRect, textContainer: NSTextContainer?) {
  16.         super.init(frame: frame, textContainer: textContainer)
  17.         commonInit()
  18.     }
  19.    
  20.     required init?(coder aDecoder: NSCoder) {
  21.         super.init(coder: aDecoder)
  22.         commonInit()
  23.     }
  24.    
  25.     private func commonInit() -> Void {
  26.         isScrollEnabled = false
  27.         isEditable = false
  28.         isSelectable = false
  29.         textContainerInset = UIEdgeInsets.zero
  30.         textContainer.lineFragmentPadding = 0
  31.     }
  32.    
  33. }
  34.  
  35. class ViewController: UIViewController {
  36.    
  37.     let labelStackView = UIStackView()
  38.     let textViewStackView = UIStackView()
  39.    
  40.     override func viewDidLoad() {
  41.         super.viewDidLoad()
  42.        
  43.         let scrollView = UIScrollView()
  44.        
  45.         let hStack = UIStackView()
  46.         hStack.axis = .horizontal
  47.         hStack.spacing = 8
  48.         hStack.distribution = .fillEqually
  49.         hStack.alignment = .top
  50.        
  51.         [labelStackView, textViewStackView].forEach { v in
  52.             v.axis = .vertical
  53.             v.spacing = 4
  54.             hStack.addArrangedSubview(v)
  55.         }
  56.        
  57.         hStack.translatesAutoresizingMaskIntoConstraints = false
  58.         scrollView.addSubview(hStack)
  59.  
  60.         scrollView.translatesAutoresizingMaskIntoConstraints = false
  61.         view.addSubview(scrollView)
  62.        
  63.         let g = view.safeAreaLayoutGuide
  64.         let cg = scrollView.contentLayoutGuide
  65.         let fg = scrollView.frameLayoutGuide
  66.  
  67.         NSLayoutConstraint.activate([
  68.            
  69.             scrollView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
  70.             scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  71.             scrollView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  72.             scrollView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
  73.  
  74.             hStack.topAnchor.constraint(equalTo: cg.topAnchor, constant: 8.0),
  75.             hStack.leadingAnchor.constraint(equalTo: cg.leadingAnchor, constant: 8.0),
  76.             hStack.trailingAnchor.constraint(equalTo: cg.trailingAnchor, constant: -8.0),
  77.             hStack.bottomAnchor.constraint(equalTo: cg.bottomAnchor, constant: -8.0),
  78.  
  79.             hStack.widthAnchor.constraint(equalTo: fg.widthAnchor, constant: -16.0),
  80.            
  81.         ])
  82.        
  83.         let infoFont: UIFont = .italicSystemFont(ofSize: 10.0)
  84.         let testFont: UIFont = .systemFont(ofSize: 12.0)
  85.        
  86.         var testStr: String = "This is some example text to test truncation consistency. At the end of this line, is a pair of newline chars:\n\nA HEADLINE\n\nThe headline was also followed by a pair of newline chars."
  87.  
  88.         for i in 3...10 {
  89.            
  90.             let infoLabel = UILabel()
  91.             infoLabel.font = infoFont
  92.             infoLabel.text = "UILabel - numLines: \(i)"
  93.             labelStackView.addArrangedSubview(infoLabel)
  94.            
  95.             let v = UILabel()
  96.             v.font = testFont
  97.             v.numberOfLines = i
  98.             v.text = testStr
  99.             v.backgroundColor = .yellow
  100.             v.lineBreakMode = .byTruncatingTail
  101.             labelStackView.addArrangedSubview(v)
  102.            
  103.             labelStackView.setCustomSpacing(16.0, after: v)
  104.            
  105.         }
  106.        
  107.         for i in 3...10 {
  108.            
  109.             let infoLabel = UILabel()
  110.             infoLabel.font = infoFont
  111.             infoLabel.text = "TextViewLabel - numLines: \(i)"
  112.             textViewStackView.addArrangedSubview(infoLabel)
  113.            
  114.             let v = TextViewLabel()
  115.             v.font = testFont
  116.             v.numberOfLines = i
  117.             v.text = testStr
  118.             v.backgroundColor = .cyan
  119.             v.lineBreakMode = .byTruncatingTail
  120.             textViewStackView.addArrangedSubview(v)
  121.            
  122.             textViewStackView.setCustomSpacing(16.0, after: v)
  123.            
  124.         }
  125.        
  126.         scrollView.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
  127.        
  128.     }
  129.    
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement