Skip to content

Instantly share code, notes, and snippets.

@DonMag
Created December 14, 2022 18:37
Show Gist options
  • Save DonMag/784a81668916ebdbbd5d7844ec09212c to your computer and use it in GitHub Desktop.
Save DonMag/784a81668916ebdbbd5d7844ec09212c to your computer and use it in GitHub Desktop.
//
// SlowLabelViewController.swift
//
// Created by Don Mag on 12/14/22.
//
// Demonstrates how slow UILabel rendering when using High-Unicode strings
class SlowLabelViewController: UIViewController {
var lowAsciiStr: String = ""
var highUnicodeStr: String = ""
var promptShowing: Bool = true
let segControl = UISegmentedControl(items: ["Low ASCII", "High Unicode"])
let theLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
let toggleBtn = UIButton()
let scrollView = UIScrollView()
[segControl, toggleBtn, theLabel, scrollView].forEach { v in
v.translatesAutoresizingMaskIntoConstraints = false
}
scrollView.addSubview(theLabel)
view.addSubview(segControl)
view.addSubview(toggleBtn)
view.addSubview(scrollView)
let g = view.safeAreaLayoutGuide
let cg = scrollView.contentLayoutGuide
let fg = scrollView.frameLayoutGuide
NSLayoutConstraint.activate([
segControl.topAnchor.constraint(equalTo: g.topAnchor, constant: 20.0),
segControl.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
segControl.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
toggleBtn.topAnchor.constraint(equalTo: segControl.bottomAnchor, constant: 20.0),
toggleBtn.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
toggleBtn.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
scrollView.topAnchor.constraint(equalTo: toggleBtn.bottomAnchor, constant: 20.0),
scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
scrollView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
scrollView.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -20.0),
theLabel.topAnchor.constraint(equalTo: cg.topAnchor, constant: 8.0),
theLabel.leadingAnchor.constraint(equalTo: cg.leadingAnchor, constant: 8.0),
theLabel.trailingAnchor.constraint(equalTo: cg.trailingAnchor, constant: -8.0),
theLabel.bottomAnchor.constraint(equalTo: cg.bottomAnchor, constant: -8.0),
theLabel.widthAnchor.constraint(equalTo: fg.widthAnchor, constant: -16.0),
])
toggleBtn.setTitle("Toggle the Text", for: [])
toggleBtn.setTitleColor(.white, for: .normal)
toggleBtn.setTitleColor(.lightGray, for: .highlighted)
toggleBtn.backgroundColor = .systemGreen
toggleBtn.layer.cornerRadius = 8.0
scrollView.backgroundColor = .systemRed
theLabel.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
theLabel.textAlignment = .center
theLabel.numberOfLines = 0
segControl.selectedSegmentIndex = 0
toggleBtn.addTarget(self, action: #selector(toggleTapped(_:)), for: .touchUpInside)
segControl.addTarget(self, action: #selector(segChanged(_:)), for: .valueChanged)
// let's create some long strings to set as the label text
var s: String = ""
let n = 50
s = ""
for i in 1...n {
s += "←↑→↓↔↕↖↗↘↙↺↻⇄⇅⇆⇇⇈⇉⇊⇋⇌⇍⇎⇏⇐⇑⇒⇓⇔⇕⇖⇗⇘⇙⇚⇛⇜⇝⇞⇟⇠⇡⇢⇣⇤⇥⇦⇧⇨⇩\nLine \(i) of \(n)\n\n"
}
highUnicodeStr = s
s = ""
for i in 1...50 {
s += "The quick red fox jumps over the lazy brown dog before he eats breakfast.\n\(i) of \(n)\n\n"
}
lowAsciiStr = s
promptShowing = false
toggleTapped(nil)
}
@objc func toggleTapped(_ sender: Any?) {
promptShowing.toggle()
if promptShowing {
if segControl.selectedSegmentIndex == 0 {
theLabel.text = "\nThis is an example of \"Low ASCII\" text.\n\nThe quick red fox jumps over the lazy brown dog before he eats breakfast.\n\nWhen we set the .text property of a UILabel it will be very fast.\n\n"
} else {
theLabel.text = "\nThis is an example of \"High Unicode\" text.\n\n←↑→↓↔↕↖↗↘↙↺↻⇄⇅⇆⇇⇈⇉⇊⇋⇌⇍⇎⇏⇐⇑⇒⇓⇔⇕⇖⇗⇘⇙⇚⇛⇜⇝⇞⇟⇠⇡⇢⇣⇤⇥⇦⇧⇨⇩\n\nWhen we set the .text property of a UILabel it will be very slow.\n\n"
}
} else {
if segControl.selectedSegmentIndex == 0 {
theLabel.text = lowAsciiStr
} else {
theLabel.text = highUnicodeStr
}
}
}
@objc func segChanged(_ sender: Any?) {
promptShowing = false
toggleTapped(nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment