Adjust UILabel height depending on the text

前端 未结 30 2302
走了就别回头了
走了就别回头了 2020-11-22 03:53

Consider I have the following text in a UILabel (a long line of dynamic text):

Since the alien army vastly outnumbers the team, players m

30条回答
  •  我寻月下人不归
    2020-11-22 04:35

    And for those that are migrating to iOS 8, here is a class extension for Swift:

    extension UILabel {
    
        func autoresize() {
            if let textNSString: NSString = self.text {
                let rect = textNSString.boundingRectWithSize(CGSizeMake(self.frame.size.width, CGFloat.max),
                    options: NSStringDrawingOptions.UsesLineFragmentOrigin,
                    attributes: [NSFontAttributeName: self.font],
                    context: nil)
                self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, rect.height)
            }
        }
    
    }
    

提交回复
热议问题