在 swift 中以编程方式在堆栈视图中添加标签和文本视图

Adding labels and textviews in a stack view programmatically in swift

如何才能以编程方式将标题、后跟几行文本、再后跟几行文本限制在视图控制器的中间?

我的目标是为标题加粗,如果 textview 行数也增加就好了。

我的想法是创建 2 个标签和 2 个文本视图。并按以下顺序将它们添加到文本视图中:label1、t1、label2、t2。

不过好像不行。我尽量避免多次定义相同的文本视图和标签。如果我复制它的定义两次而不是标签(也许它与视图相关?)

我尝试使用 UIbuttons 并且成功了。

这是我到目前为止尝试过的:

import UIKit

class HowToSetupProIGVC: UIViewController {
    deinit {print("deinit")}

    let textView: UITextView = {
        let textView = UITextView()
        textView.backgroundColor = .blue //bkgdColor
        textView.textAlignment = .left
        //textView.frame = CGRect(x: 5, y: 5, width: 5, height: 5)
        textView.tintColor = .black
        textView.translatesAutoresizingMaskIntoConstraints = false //enable autolayout
        textView.heightAnchor.constraint(equalToConstant: 100).isActive = true
        textView.widthAnchor.constraint(equalToConstant: 300).isActive = true
        return textView
    }()
    
    let label: UILabel = {
        let l = UILabel(frame:CGRect.zero)
        //l.frame = CGRect(x: 5, y: 5, width: 5, height: 5)
        l.backgroundColor = .green //bkgdColor
        l.font = UIFont.preferredFont(forTextStyle: .headline)
        l.translatesAutoresizingMaskIntoConstraints = false //enable autolayout
        l.heightAnchor.constraint(equalToConstant: 22).isActive = true
        l.widthAnchor.constraint(equalToConstant: 300).isActive = true
        return l
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.modalUI(arrowButton: false)
        self.view.backgroundColor = bkgdColor
        customStackHTSProIG ()

    }
    
    
}

extension HowToSetupProIGVC {
    
    func customStackHTSProIG () {
        
        let label1 = label
        let label2 = label
        
        let t1 = textView
        let t2 = textView
        
        label1.text = "Title1:"
        label2.text = "title2:"
        
        t1.text = """
            1. On your profile tap menu
            2. Tap settings
            3. Tap accounts
            4. Tap set up professional account
            """
            
        t2.text = """
            1. On your profile tap "Edit profile"
            2. Link your created page to your account
            """
        
        //StackView
        let stackHTS = UIStackView()
        stackHTS.axis = NSLayoutConstraint.Axis.vertical
        stackHTS.distribution = .fillEqually
        stackHTS.alignment = .center
        stackHTS.spacing = 5
        stackHTS.backgroundColor = .red
       
        
        //Add StackView + elements
        stackHTS.addArrangedSubview(label1)
        stackHTS.addArrangedSubview(t1)
        stackHTS.addArrangedSubview(label2)
        stackHTS.addArrangedSubview(t2)
        
        self.view.addSubview(stackHTS)
        
        
        //Constraints StackView
        stackHTS.translatesAutoresizingMaskIntoConstraints  = false
        stackHTS.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
        stackHTS.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
        //stackHTS.heightAnchor.constraint(equalToConstant: 88).isActive = true
    }
    
}

UILabel & UITextView 都是用 Objective-C 编写的 UIKit 类。它们是引用类型,而不是值类型。

当你写以下内容时 -

let label1 = label
let label2 = label
        
let t1 = textView
let t2 = textView

label1label2 都指向 UILabel 的同一个实例。 t1 & t2 也是如此。

当你像这样添加它们时 -

//Add StackView + elements
stackHTS.addArrangedSubview(label1)
stackHTS.addArrangedSubview(t1)
stackHTS.addArrangedSubview(label2)
stackHTS.addArrangedSubview(t2)

您希望将 2 个标签和 2 个文本视图添加到 StackView。不过,您只添加了 1 个标签和 1 个文本视图。

您希望看到以下所有内容 -

        label1.text = "Title1:"
        label2.text = "title2:"
        
        t1.text = """
            1. On your profile tap menu
            2. Tap settings
            3. Tap accounts
            4. Tap set up professional account
            """
            
        t2.text = """
            1. On your profile tap "Edit profile"
            2. Link your created page to your account
            """

然而你只看到以下 -

        label2.text = "title2:"
        
        t2.text = """
            1. On your profile tap "Edit profile"
            2. Link your created page to your account
            """

解决方案-

  1. 创建两个单独的 UITextViewUILabel 实例,就像您为前两个所做的那样,并将这些新实例也添加到堆栈视图中。
  2. 使用一个 UILabel 并删除所有其他内容。使用 NSAttributedString API 根据需要为不同的部分/段落设置文本样式,并将其分配给 UILabel.attributedText