Determine the height of the UILabel to center when using adjustFontSizeToFitWidth

I have the following cell design where the numerical label is compressed and the โ€œGeneralโ€ label is underneath.

enter image description here

I set the adjustFontSizeToFitWidth and minimumFontSize properties minimumFontSize . The font changes correctly. However, linking a numeric tag to the base is a complex task. In particular, when the font is compressed, the gap between the two labels expands and is not displayed in vertical orientation.

I tried sizeToFit , sizeThatFits and used pointSize font. All to no avail.

I know sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode: but I donโ€™t understand why I need this in combination with adjustFontSizeToFitWidth .

+6
source share
2 answers

Ah, so you want to put UILabels in the middle of the container view (both horizontally and vertically)?

I rephrased my answer, so it will make more sense to future readers.

My code assumes you have 3 IBOutlets installed:

 UIView *containerView; //Your nice view containing the two textfields. UILabel *points; //The label containing the number. UILabel *overall; //The textfield containing the text 'overall'. 

You can simply set the label frame after by assigning text and calling sizeToFit .

  • This first line positions the UILabel points, the only change is that the y coordinate is half the containerView subtracts half the height itself.

     points.frame = CGRectMake(points.frame.origin.x, (containerView.frame.size.height / 2) - (points.frame.size.height / 2), points.frame.size.width, points.frame.size.height); 
  • To put overall accordingly - let's say that there is a distance of 6 between the quantity and the total marks:

     int space = 6; overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height); 
  • After reading your comments, I think you are after this decision. If you want both UILabels displayed in the middle; subtract (overall.frame.size.height / 2) + (space / 2) from the y points value in the same way (with the code at number 2 below):

     int space = 6; points.frame = CGRectMake(points.frame.origin.x, ((containerView.frame.size.height / 2) - (points.frame.size.height / 2)) - ((overall.frame.size.height / 2) + (space / 2)), points.frame.size.width, points.frame.size.height); overall.frame = CGRectMake(overall.frame.origin.x, points.frame.origin.y + points.frame.size.height + space, overall.frame.size.width, overall.frame.size.height); 

The endpoint will produce a result similar to this image. Since you can see that the blue line is half the entire image, and intersects the black rectangle (which is placed around the two labels) at its midpoint. Hope this is what you were.

image depicting effect

+2
source

Instead of two labels, use CATextLayer . You can easily make one part of BOLD and the other normal. Plus the position and adjustment size for one layer will be easy relative to the placement of the two marks. shadow setting, line break mode, fonts that you can fully adjust :)

0
source

All Articles