Advertisement
Don_Mag

Untitled

Dec 3rd, 2023 (edited)
1,089
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.30 KB | None | 0 0
  1.  
  2. // UIStackView using .fillProportionally
  3.  
  4. class ProportionalStackVC: UIViewController {
  5.    
  6.     let proLeft: UILabel = {
  7.         let v = UILabel()
  8.         v.textAlignment = .left
  9.         v.lineBreakMode = .byTruncatingTail
  10.         v.backgroundColor = .cyan
  11.         return v
  12.     }()
  13.  
  14.     let proRight: UILabel = {
  15.         let v = UILabel()
  16.         v.textAlignment = .right
  17.         v.lineBreakMode = .byTruncatingHead
  18.         v.backgroundColor = .yellow
  19.         return v
  20.     }()
  21.  
  22.     let sampleStrings: [[String]] = [
  23.         ["My text is less than half.", "Less than half."],
  24.         ["Less than half.", "My text is less than half."],
  25.         ["This text is a bit wider than half.", "Less than half."],
  26.         ["Less than half.", "This text is a bit wider than half."],
  27.         ["This text is considerably wider than half.", "Less than half."],
  28.         ["Less than half.", "This text is considerably wider than half."],
  29.         ["This text is considerably wider than half.", "Text still Less than half."],
  30.         ["Text still Less than half.", "This text is considerably wider than half."],
  31.         ["The Left Label is considerably wider than half.", "Right Label is wider than half."],
  32.         ["Left Label is wider than half.", "The Right Label is considerably wider than half."],
  33.         ["This text is just about but not quite full width.", "Short."],
  34.         ["Short.", "This text is just about but not quite full width."],
  35.     ]
  36.  
  37.     var idx: Int = 0
  38.    
  39.     override func viewDidLoad() {
  40.         super.viewDidLoad()
  41.        
  42.         let stackView = UIStackView()
  43.         stackView.distribution = .fillProportionally
  44.         stackView.spacing = 10
  45.        
  46.         stackView.addArrangedSubview(proLeft)
  47.         stackView.addArrangedSubview(proRight)
  48.  
  49.         stackView.translatesAutoresizingMaskIntoConstraints = false
  50.         view.addSubview(stackView)
  51.        
  52.         let g = view.safeAreaLayoutGuide
  53.         NSLayoutConstraint.activate([
  54.             stackView.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
  55.             stackView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 8.0),
  56.             stackView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -8.0),
  57.         ])
  58.        
  59.         updateLabels()
  60.     }
  61.    
  62.     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  63.         updateLabels()
  64.     }
  65.  
  66.     func updateLabels() {
  67.        
  68.         let p = sampleStrings[idx % sampleStrings.count]
  69.        
  70.         proLeft.text = p[0]
  71.         proRight.text = p[1]
  72.  
  73.         idx += 1
  74.         if idx == sampleStrings.count {
  75.             idx = 0
  76.         }
  77.        
  78.     }
  79.    
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement