UILabel 文字靠左靠上對齊

使用Xcode Swift 開發iPhone App。針對UILabel 上的文字,我們可以透過繼承UILabel ,override 一個function,即可以達成文字靠左靠上對齊。詳細程式碼如下:

import UIKit

class TopAlignedLabel: UILabel {
  override func drawText(in rect: CGRect) {
    let textRect = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
    super.drawText(in: textRect)
  }
}

發佈留言