Don_Mag icon

Untitled

Don_Mag | PRO | 12/03/23 01:26:46 PM UTC (Edited) | 0 ⭐ | 8556 👁️ | Never ⏰ | []
Swift |

2.3 KB

|

None

|

0 👍

/

0 👎

 
// UIStackView using .fillProportionally
 
class ProportionalStackVC: UIViewController {
    
    let proLeft: UILabel = {
        let v = UILabel()
        v.textAlignment = .left
        v.lineBreakMode = .byTruncatingTail
        v.backgroundColor = .cyan
        return v
    }()
 
    let proRight: UILabel = {
        let v = UILabel()
        v.textAlignment = .right
        v.lineBreakMode = .byTruncatingHead
        v.backgroundColor = .yellow
        return v
    }()
 
    let sampleStrings: [[String]] = [
        ["My text is less than half.", "Less than half."],
        ["Less than half.", "My text is less than half."],
        ["This text is a bit wider than half.", "Less than half."],
        ["Less than half.", "This text is a bit wider than half."],
        ["This text is considerably wider than half.", "Less than half."],
        ["Less than half.", "This text is considerably wider than half."],
        ["This text is considerably wider than half.", "Text still Less than half."],
        ["Text still Less than half.", "This text is considerably wider than half."],
        ["The Left Label is considerably wider than half.", "Right Label is wider than half."],
        ["Left Label is wider than half.", "The Right Label is considerably wider than half."],
        ["This text is just about but not quite full width.", "Short."],
        ["Short.", "This text is just about but not quite full width."],
    ]
 
    var idx: Int = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let stackView = UIStackView()
        stackView.distribution = .fillProportionally
        stackView.spacing = 10
        
        stackView.addArrangedSubview(proLeft)
        stackView.addArrangedSubview(proRight)
 
        stackView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stackView)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            stackView.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
            stackView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 8.0),
            stackView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -8.0),
        ])
        
        updateLabels()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        updateLabels()
    }
 
    func updateLabels() {
        
        let p = sampleStrings[idx % sampleStrings.count]
        
        proLeft.text = p[0]
        proRight.text = p[1]
 
        idx += 1
        if idx == sampleStrings.count {
            idx = 0
        }
        
    }
    
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎