代码之家  ›  专栏  ›  技术社区  ›  BigBoy1337

如何在tableViewCell中居中放置textView?

  •  -1
  • BigBoy1337  · 技术社区  · 7 年前

    我有一个静态的表视图,我正在这样创建。。。。

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

    现在看来 enter image description here

    tipsText.center = CGPoint(x: tipsCell.bounds.midX, y: tipsCell.bounds.midY)

    正如你所看到的,这是不工作的,因为提示文字仍然是在顶部的提示。我怎么能把它放在我想要的中心呢?

    2 回复  |  直到 7 年前
        1
  •  1
  •   E.Coms    7 年前

    一种方法是将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)
    
        2
  •  0
  •   EmreSURK    7 年前

    我假设您知道如何使用自定义视图类。 UITextView有一个自定义类;

    class VerticallyCenteredTextView: UITextView {
        override var contentSize: CGSize {
            didSet {
                var topCorrection = (bounds.size.height - contentSize.height * zoomScale) / 2.0
                topCorrection = max(0, topCorrection)
                contentInset = UIEdgeInsets(top: topCorrection, left: 0, bottom: 0, right: 0)
            }
        }
    }