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

hlswsv35  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(146)

我怎样才能让一个标题,后面跟着几行文本,再后面跟着一个标题,再后面跟着几行文本,以编程方式限制在视图控制器的中间?
我的目标是加粗标题,并且增加textview行也很好。
我的想法是创建两个标签和两个文本视图。然后按以下顺序将它们添加到textview:标签1、t1、标签2、t2。
但好像不管用。我尽量避免多次定义相同的文本视图和标签。如果我复制它的定义两次,textviews就会增加,但标签不会(也许它与视图相关?))
我试过使用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
    }
    
}
lf3rwulv

lf3rwulv1#

UILabelUITextView都是用Objective-C编写的UIKit类。它们是引用类型,而不是值类型。
当你写下-

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

label1label2都指向UILabel的同一个示例。t1t2也是如此。
当你把它们像这样加在一起-

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

您希望将2个标签和2个textView添加到StackView。您只添加了1个标签和1个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
            """

但你只能看到以下内容--

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

解决方案-
1.创建UITextViewUILabel的两个独立示例,就像您已经为前两个示例所做的那样,并将这些新示例添加到堆栈视图中。
1.使用一个UILabel并删除其他所有内容。使用NSAttributedString API为不同的部分/段落样式化文本,并将其分配给UILabel.attributedText

相关问题