如何在tableViewCell中居中textView?

大男孩1337

我有一个我正在创建的静态 tableView....

 override func viewDidLoad() {
        super.viewDidLoad()

        usernameLabel.text = "Username"
        usernameDisplay.text = "placeholder"
        usernameDisplay.textColor = self.view.tintColor
        usernameLabel.frame = CGRect(x: self.view.bounds.minX, y: self.usernameCell.bounds.minY, width: self.view.frame.width, height: self.usernameCell.bounds.height)
        usernameDisplay.frame = CGRect(x: self.usernameCell.bounds.minX, y: self.usernameCell.bounds.minY, width: self.view.frame.width, height: self.usernameCell.bounds.height)
        usernameDisplay.textAlignment = .right
        usernameCell.addSubview(usernameLabel)
        usernameCell.addSubview(usernameDisplay)

        membershipTierLabel.text = "Membership Status"
        membershipTierDisplay.text = "Free (0-1GB)"
        membershipTierDisplay.textColor = self.view.tintColor
        membershipTierLabel.frame = CGRect(x: self.view.bounds.minX, y: self.membershipTierCell.bounds.minY, width: self.view.frame.width, height: self.membershipTierCell.bounds.height)
        membershipTierDisplay.frame = CGRect(x: self.view.bounds.minX, y: self.membershipTierCell.bounds.minY, width: self.view.frame.width, height: self.membershipTierCell.bounds.height)
        membershipTierDisplay.textAlignment = .right
        membershipTierCell.addSubview(membershipTierLabel)
        membershipTierCell.addSubview(membershipTierDisplay)

        dataUsedLabel.text = "Data Used"
        dataUsedDisplay.text = String(UserDefaults.standard.integer(forKey: "total_storage"))
        dataUsedDisplay.textColor = self.view.tintColor
        dataUsedLabel.frame = CGRect(x: self.view.bounds.minX, y: self.dataUsedCell.bounds.minY, width: self.view.frame.width, height: self.dataUsedCell.bounds.height)
        dataUsedDisplay.frame = CGRect(x: self.view.bounds.minX, y: self.membershipTierCell.bounds.minY, width: self.view.frame.width, height: self.membershipTierCell.bounds.height)
        dataUsedDisplay.textAlignment = .right
        dataUsedCell.addSubview(dataUsedLabel)
        dataUsedCell.addSubview(dataUsedDisplay)

        tipsText.text = "You can change the color of a progression tag by long-pressing it on the Progressions homepage"

        tipsText.frame = CGRect(x: tipsCell.bounds.minX,y: self.tipsCell.bounds.minY,width: tipsCell.bounds.width,height:self.tipsCell.bounds.height)
        //tipsText.textColor = UIColor.white
        //tipsText.textAlignment = .center
        tipsText.backgroundColor = self.view.tintColor
        tipsText.font = tipsText.font?.withSize(15)
        tipsText.autoresizingMask = [.flexibleWidth, .flexibleHeight]

//        tipsText.centerYAnchor.constraint(equalTo: tipsCell.centerYAnchor, constant:0).isActive = true
//        tipsText.centerXAnchor.constraint(equalTo: tipsCell.centerXAnchor, constant:0).isActive = true
        //tipsText.textContainerInset = UIEdgeInsets(top: 10, left: 5, bottom: 10, right: 5)
        tipsCell.backgroundColor = self.view.tintColor
        tipsCell.addSubview(tipsText)
        tipsText.center = CGPoint(x: tipsCell.bounds.midX, y: tipsCell.bounds.midY)

        feedbackText.text = "Feedback is loved! Please email with any issues, comments, ideas, or concerns"
        feedbackText.frame = CGRect(x: self.feedbackCell.bounds.minX,y: self.feedbackCell.bounds.minY,width: self.feedbackCell.bounds.width,height:self.feedbackCell.bounds.height)
        feedbackText.font = feedbackText.font?.withSize(15)
        feedbackCell.addSubview(feedbackText)

        //let bytes = UserDefaults.standard.integer(forKey: "total_storage")
        //storageAmount.text = format(bytes:Double(bytes))
        // Do any additional setup after loading the view.
        //myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
        myTableView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
        myTableView.dataSource = self
        myTableView.delegate = self
        self.view.addSubview(myTableView)
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section != 0 {
            return 100
        } else {
            return UITableViewAutomaticDimension
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch(section) {
        case 0: return 3    // section 0 has 2 rows
        case 1: return 1
        case 2: return 1// section 1 has 1 row
        default: fatalError("Unknown number of sections")
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch(indexPath.section) {
        case 0:
            switch(indexPath.row){
            case 0:
                print("username")
                return self.usernameCell
            case 1:
                return self.membershipTierCell
            case 2:
                return self.dataUsedCell
            default: fatalError("Unknown row")
            }
        case 1:
            switch(indexPath.row){
            case 0:
                return self.tipsCell
            default: fatalError("Unknown row")
            }
        case 2:
            print("feedback")
            switch(indexPath.row){
            case 0:
                print("feedback")
                return self.feedbackCell
            default: fatalError("Unknown row")
            }
        default: fatalError("Unknown section")
        }
    }
}

现在看起来像 在此处输入图片说明

如您所见,我正在尝试使用以下方法在提示单元的正中央设置提示文本(UITextView): tipsText.center = CGPoint(x: tipsCell.bounds.midX, y: tipsCell.bounds.midY)

如您所见,这不起作用,因为tipsText 仍位于tipsCell 的顶部。我怎样才能像我想要的那样将它居中?

电子商务公司

我认为你有两种方法可以实现你的愿望。

一种方法是在IB中将tipsCell和feedBackCell的高度设置为100(如果有)。

另一种方法是添加如下代码:

 tipsText.textAlignment = .center
    tipsText.numberOfLines = 0
    tipsText.autoresizingMask = [.flexibleWidth, .flexibleHeight]

  feedbackText.textAlignment = .center
   feedbackText.numberOfLines = 0
     feedbackText.autoresizingMask = [.flexibleWidth, .flexibleHeight]

希望它有帮助。

如果是 TextView,你可能会得到这样的估计帧:

    tipsText.textContainer.maximumNumberOfLines = 0
    tipsText.textContainer.heightTracksTextView = true
    tipsText.attributedText = NSAttributedString.init(string:  "You can change the color of a progression tag by long-pressing it on the Progressions", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15) ])
    tipsText.textAlignment = .center
    let size = tipsText.textContainer.size
    tipsText.frame = CGRect(x: tipsCell.bounds.minX,y: self.tipsCell.bounds.minY,width: tipsCell.bounds.width,  height :size.height )
    tipsText.backgroundColor = self.view.tintColor
    tipsText.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    tipsCell.backgroundColor = self.view.tintColor
    tipsCell.addSubview(tipsText)
    tipsText.center = CGPoint(x: tipsCell.bounds.midX, y: tipsCell.bounds.midY)


    feedbackText.textContainer.maximumNumberOfLines = 0
    feedbackText.textContainer.heightTracksTextView = true
    feedbackText.attributedText = NSAttributedString.init(string:  "Feedback is loved! Please email with any issues, comments, ideas, or concerns", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 15) ])
    feedbackText.textAlignment = .justified
    let feedbackSize = feedbackText.textContainer.size
    feedbackText.frame = CGRect(x: feedbackCell.bounds.minX,y: self.feedbackCell.bounds.minY,width: feedbackCell.bounds.width,  height :feedbackSize.height )
    feedbackText.backgroundColor = self.view.tintColor
    feedbackText.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    feedbackCell.backgroundColor = self.view.tintColor
    feedbackCell.addSubview(feedbackText)
    feedbackText.center = CGPoint(x: feedbackCell.bounds.midX, y: feedbackCell.bounds.midY)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章