gpt4 book ai didi

ios - 创建一个函数以在 Swift4 中从字符串数组以编程方式创建 UILabel

转载 作者:搜寻专家 更新时间:2023-11-01 06:52:58 25 4
gpt4 key购买 nike

我能够以编程方式创建一个 UILabel,但是当我创建一个函数时,我必须将它放在 viewDidLoad() 方法中吗?我需要使用 self.view.addSubview(label) 将标签附加到 View ,但它会引发错误,然后当它在 viewDidLoad() 之外时我的构建失败

每当我尝试在 viewDidLoad 方法之外创建函数时,它都会声明“使用未解析的标识符自身”。有没有办法解决?

此外,当我将函数放在 viewDidLoad 中时,一切正常,并且创建了标签,但它们是在彼此之上创建的。如果可能,我希望标签彼此相邻?

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

func createALabel(inputtedString: String) -> Void {
let string = inputtedString
let test = string.components(separatedBy: " ")

for element in test {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 60, height: 35))
label.center = CGPoint(x: 160, y: 285)
label.textAlignment = .center
label.textColor = UIColor.blue
label.text = element
label.layer.borderColor = UIColor.blue.cgColor
label.layer.borderWidth = 3.0
self.view.addSubview(label)
}
}
}

Even with the position change my labels are still stacking

最佳答案

使用 Collection View 是最好的选择。它使您的工作更轻松。试试这个

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
var wordsArr = [String]()

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
collectionView.backgroundColor = .white
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(WordCell.self, forCellWithReuseIdentifier: "WordCell")
collectionView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(collectionView)
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(5)-[collectionView]-(5)-|", options: [], metrics: nil, views: ["collectionView":collectionView]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(5)-[collectionView]-(5)-|", options: [], metrics: nil, views: ["collectionView":collectionView]))

createALabel(inputtedString: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam")
}
func createALabel(inputtedString: String) -> Void {
wordsArr = inputtedString.components(separatedBy: " ")
collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return wordsArr.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WordCell", for: indexPath) as? WordCell ?? WordCell()
cell.label.text = wordsArr[indexPath.row]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = (wordsArr[indexPath.row] as NSString).boundingRect(with: CGSize.zero, options: .usesLineFragmentOrigin, attributes: [.font: UIFont.systemFont(ofSize: 16.0)], context: nil).size.width
let size = CGSize(width: width + 10
, height: 35)
return size
}

}
class WordCell: UICollectionViewCell {

let label = UILabel()

override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {

backgroundColor = .white

label.font = UIFont.systemFont(ofSize: 16.0)
label.textAlignment = .center
label.textColor = UIColor.blue
label.layer.borderColor = UIColor.blue.cgColor
label.layer.borderWidth = 3.0
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)

addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[label(35)]|", options: [], metrics: nil, views: ["label":label]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[label]|", options: [], metrics: nil, views: ["label":label]))
}
}

enter image description here

关于ios - 创建一个函数以在 Swift4 中从字符串数组以编程方式创建 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55897744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com