Vertically align text within a UILabel (Note : Using AutoLayout)

前端 未结 17 2697
忘掉有多难
忘掉有多难 2020-12-23 02:24

I am Copying the same Question asked Before Question. I have tried the solutions given and was not able to solve it since sizetofit was not effective when I

17条回答
  •  青春惊慌失措
    2020-12-23 02:47

      @IBInspectable var alignTop: Bool = false
    
      func setAlignTop() {
        let text = self.text!
        let lines = text.characters.split(separator: "\n").count
        if lines < self.numberOfLines {
          var newLines = ""
          for _ in 0..<(self.numberOfLines - lines) {
            newLines = newLines.appending("\n ")
          }
          self.text! = text.appending(newLines)
        }
      }
    
      override var text: String? {
        didSet {
          if alignTop {
            self.setAlignTop()
          }
        }
      }
    

提交回复
热议问题