2
votes

I have a number of UILabels, all of which have adjustsFontSizeToFitWidth set to true. They are all different sizes, and the length of the text inside them will be different, so essentially the size of the text in each label can be arbitrary.

My goal is to have the text size be the same for all of them, so that after each label determines how small its text must be in order to fit, they all resize their text to the size of the smallest font size among the UILabels. This is the largest font size that is small enough for each label's text to fit.

I have also considered manually computing, for each label, the largest font size that will allow its text to fit, and then setting each label's font size to the smallest of those font sizes, but I don't know how to do this computation.

1
Could you loop through the subviews - check only the UILabels - pulling out the font size - and then looping a second time, setting each label to the font size you need?dfd
You may have to compute things manually. I just set up a test of my suggestion and it appears that the font size adjusting to fit isn't reflected in the UILabel.font.pointSize value. They all came back as 17. :-/dfd
@dfd I think that's right, the font size shown after shrinking bears no relation to the font size stored in the label's font (except that it's no larger than). Do you know how to compute what the final size would be before the cells lay out their text? This would solve my problem.BallpointBen
Unfortunately no. I had some code - lost over the last year - that computed the width of a font size (possible Google search) - but it's lost. However, check out this link: stackoverflow.com/questions/8812192/…. The second listed answer is promising to compute the height/width of text without too much trial and error to hit the "perfect" font size.. I'm going to bookmark this for future reference.dfd

1 Answers

0
votes

You'll have to iterate over your labels to find the required minimum size. Check this code

    let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 10))
    let textArray = ["asdasdas","asdsd","asdas"]
    let size : CGFloat = 14
    var minimumSize = size
    for string in textArray{
        var i : CGFloat = 0
        while size-i>0 {
            let font = UIFont.systemFont(ofSize: size-i)
            let preferredSize = label.preferredLabelSize(maximumFrameBounds: view.frame, font: font, text: string)
            if label.frame.size.width>=preferredSize.width{
                minimumSize = min(size-i, minimumSize)
                print(minimumSize)
                break
            }
            i += 1
        }
        print(minimumSize)
    }

I'm just iterating over an array of string on the same label to find the minimum size of the font that needs to be displayed. Not sure if this will work perfectly, but this will point you in the right direction. You'll need to iterate over your labels with their text to find the minimum font size instead of iterating over the array of string. You can try modifying the width of the label or the string in the array to see if it works as you want it to. Also, I would suggest you use auto-layout if you can and let the OS deal with the size instead of manually calculating the font size.