Создание функции для программного создания UILabels из String Array в Swift4 - PullRequest
1 голос
/ 29 апреля 2019

Я могу программно создать UILabel, но когда я создаю функцию, я должен поместить ее в метод viewDidLoad()?Мне нужно добавить метку к представлению, используя self.view.addSubview(label), но оно выдает ошибку, и моя сборка завершается ошибкой, когда она выходит за пределы viewDidLoad().

Всякий раз, когда я пытаюсь создать функцию внеметод viewDidLoad гласит «использование неразрешенного идентификатора self».Есть ли что-нибудь вокруг этого?

Кроме того, когда я помещаю функцию в 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)
        }
    }
}

Даже с изменением положения мои надписи все еще складываются

Ответы [ 3 ]

1 голос
/ 29 апреля 2019

Для управления такой геометрией UIStackView может быть полезно. Также вам не нужно вызывать этот метод в viewDidLoad, метод жизненного цикла loadView более подходит для этого, когда вы хотите инициализировать и управлять своими представлениями. программно.

class ViewController: UIViewController {
    private let vstackView = UIStackView()
    private let horizontal_spacing = 20.0
    private let spaceConstantForLetter = 10.0

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        vstackView.axis = .vertical
        vstackView.translatesAutoresizingMaskIntoConstraints = false
        vstackView.alignment = .center
        vstackView.spacing = 10

        self.view.addSubview(vstackView)
        vstackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        vstackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true


        createALabel(inputtedString: "this is a long test string that exceeds screen width")//
        // Do any additional setup after loading the view.

    }


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


        var horizontalStackList = [UILabel] ()

        for (index, element) in test.enumerated() {
            let label = UILabel()
            label.textAlignment = .center
            label.textColor = UIColor.blue
            label.text = element
            label.layer.borderColor = UIColor.blue.cgColor
            label.layer.borderWidth = 1.0
            horizontalStackList.append(label)

            let totalLetterNumber = horizontalStackList.map { $0.text!.count }.reduce(0, +)

            let contentWidth = Double(totalLetterNumber) * spaceConstantForLetter //constant for a letter that takes how much space from screen
            let spacingWidth = Double(horizontalStackList.count - 1) * horizontal_spacing
            /*add left and right margins if exist*/
            if((CGFloat(spacingWidth + contentWidth) > UIScreen.main.bounds.width)  || index == test.count - 1) {
                let exceedingLabel = horizontalStackList.popLast()
                let hstackView = UIStackView(arrangedSubviews: horizontalStackList)
                hstackView.axis = .horizontal
                hstackView.translatesAutoresizingMaskIntoConstraints = false
                hstackView.spacing = CGFloat(horizontal_spacing)
                hstackView.alignment = .center
                vstackView.addArrangedSubview(hstackView)
                horizontalStackList.removeAll(keepingCapacity: false)
                horizontalStackList.append(exceedingLabel!)
            }

        }
    }
}

Редактировать: добавление переноса слов по желанию Jazzin

output of the code above

1 голос
/ 29 апреля 2019

Использование представления коллекции - лучший вариант.Это облегчает вашу работу.Попробуйте это

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

0 голосов
/ 29 апреля 2019

Вы должны добавить одну целочисленную переменную, чтобы установить позицию x. Здесь я установил переменную xPos в соответствии с заданным положением x

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: " ")

             let xPos = 0
            for element in test {
                let label = UILabel(frame: CGRect(x: xPos, y: 0, width: 60, height: 35))
               // label.center = CGPoint(x: 160, y: 285)
                label.textAlignment = .center
                xPos = xPos + 60 + 10   // Update xPos width 60 & 10 is gap between two label
                label.textColor = UIColor.blue
                label.text = element
                label.layer.borderColor = UIColor.blue.cgColor
                label.layer.borderWidth = 3.0
                self.view.addSubview(label)
            }
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...