iosswiftcgfloatcgsize

How to get CGFloat/CGSize based on String length?


I have a UIView, which acts as a container and the width of this container needs to change dynamically based on the longest String. So I want to get CGFloat/CGSize based on String length, is it still possible ?

The method which seems most helpful is now depreciated...

myStringSize : CGSize  = [longestString sizeWithFont:myFont 
                       constrainedToSize:maximumSize 
                       lineBreakMode:self.myLabel.lineBreakMode];

Rather than basing it on a label I would like to base it on a String


Solution

  • Thank you for your help @Venkat, this is the solution that worked for me

     @objc func containerWidth() -> (CGFloat){
            let containerSize = self.myLabel.intrinsicContentSize
            containerWidth = containerSize.width
            return containerWidth
        }