Get the font UILabel font after minimumFontSize

I have a UILabel with a dot size font of 17. If I call label.font.pointSize, I get 17, which is good. BBUUUUTTT I also have a minimum set of settings equal to 8, now if I roll some text in the label that causes the size of the dot to decrease and then call label.font.pointsize, I still get 17, although I know that the size of the dot less

Any ideas on how to get the true point size after the system has changed the font size?

+6
source share
3 answers

As savner noted in the comments, this is a duplication issue. The cleanest solution can be found here: How to get automatic font size UILabel (UITextView)? . However, the Sanjit solution also works! Thanks everyone!

CGFloat actualFontSize; [label.text sizeWithFont:label.font minFontSize:label.minimumFontSize actualFontSize:&actualFontSize forWidth:label.bounds.size.width lineBreakMode:label.lineBreakMode]; 
+2
source

I do not know the API to get the current size of a UILabel point when it reduces your content down. You can try to approach the “scaling factor” using the sizeWithFont API.

Just an idea:

 // Get the size of the text with no scaling (one line) CGSize sizeOneLine = [label.text sizeWithFont:label.font]; // Get the size of the text enforcing the scaling based on label width CGSize sizeOneLineConstrained = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size]; // Approximate scaling factor CGFloat approxScaleFactor = sizeOneLineConstrained.width / sizeOneLine.width; // Approximate new point size CGFloat approxScaledPointSize = approxScaleFactor * label.font.pointSize; 
+4
source

Swift 4 and iOS sizeWithFont ( sizeWithFont now deprecated) @Sanjit Saluja answer:

 // Get the size of the text with no scaling (one line) let sizeOneLine: CGSize = label.text!.size(withAttributes: [NSAttributedStringKey.font: label.font]) // Get the size of the text enforcing the scaling based on label width let sizeOneLineConstrained: CGSize = label.text!.boundingRect(with: label.frame.size, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: label.font], context: nil).size // Approximate scaling factor let approxScaleFactor: CGFloat = sizeOneLineConstrained.width / sizeOneLine.width // Approximate new point size let approxScaledPointSize: CGFloat = approxScaleFactor * label.font.pointSize 
0
source

Source: https://habr.com/ru/post/946519/


All Articles