Skip to content

Instantly share code, notes, and snippets.

@bob910078
Created July 2, 2020 03:37
Show Gist options
  • Save bob910078/109dbc6f95f8e5fddf63c671977c1f91 to your computer and use it in GitHub Desktop.
Save bob910078/109dbc6f95f8e5fddf63c671977c1f91 to your computer and use it in GitHub Desktop.
class ViewController: UIViewController {
let str = """
Hong Kong (CNN Business)This was supposed to be the year that Beijing would announce it had ended absolute poverty in China, fulfilling a key pledge of President Xi Jinping and one of the founding missions of the Chinese Communist Party.
Then the novel coronavirus pandemic tore through the global economy.
China shut down factories in January and February to prevent a larger outbreak and contain the virus, but at the same time it severely damaged employment and production domestically.
Consequently, China's economy shrank by 6.8% in the first quarter of 2020 compared to a year earlier. For the first time in decades, Beijing didn't set a GDP target at its annual meeting of its rubber-stamp parliament, the National People's Congress (NPC), in May.
Since then, China has been trying to dig itself out of its economic slump, and there are some signs of recovery — though the path forward is still looking slow and painful.
Facing reporters at his tightly controlled annual press conference at the end of the NPC, Chinese Premier Li Keqiang admitted that some Chinese citizens had been impoverished by the pandemic.
"Before Covid-19 struck, there were some five million people living below the poverty line. But because of the disease, some may have fallen back into poverty," Li said. "Hence, we now face a greater task in meeting our goal."
Experts say Xi's chances of hitting his grand goal largely hinge on whether or not Beijing can get the country back to work quickly.
"As long as employment continues to recover or mostly recovers by the end of the year, they're going to come really close to hitting their poverty targets," said Scott Rozelle, co-director of Stanford University's Rural Education Action Program.
"[But] there's just a lot of things that say this is going to be a persistent problem."
"""
var viewModel: [String] = []
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.estimatedItemSize = CGSize(width: 1, height: 1)
let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
view.translatesAutoresizingMaskIntoConstraints = false
view.register(LabelCollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
view.dataSource = self
view.backgroundColor = .clear
return view
}()
private lazy var collectionViewConstraints: [NSLayoutConstraint] = [
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setupLayoutConstraints()
viewModel = str.split(separator: " ").map({ String($0) })
collectionView.reloadData()
}
private func setupLayoutConstraints() {
view.addSubview(collectionView)
NSLayoutConstraint.activate(collectionViewConstraints)
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return viewModel.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! LabelCollectionViewCell
cell.label.text = viewModel[indexPath.item]
return cell
}
}
class LabelCollectionViewCell: UICollectionViewCell {
var label: UILabel = {
let view = UILabel()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private lazy var labelConstraints: [NSLayoutConstraint] = [
label.topAnchor.constraint(equalTo: contentView.topAnchor),
label.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
label.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
]
override init(frame: CGRect) {
super.init(frame: frame)
setupLayoutConstraints()
adaptToUserInterfaceStyle()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupLayoutConstraints() {
contentView.addSubview(label)
NSLayoutConstraint.activate(labelConstraints)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment