In a simple iPhone app I display a letter tile (custom UIView
with an image and 2 labels) by the following code in viewDidLoad:
DraggedTile *tile = [[[NSBundle mainBundle] loadNibNamed:@"DraggedTile" owner:self options:nil] firstObject]; tile.frame = CGRectMake(10 + arc4random_uniform(100), 10 + arc4random_uniform(100), kWidth, kHeight); [self.view addSubview:tile];
This works okay, but I would like to make the letter tile grow - when I supply bigger width and height parameters to the CGRectMake
.
So in Xcode 5.1 Interface Builder I open the DraggedTile.xib
and enable "Auto Layout".
Then for the image
and letter
I add constraints to the left, top, right, bottom edges of the parent.
For the letter
label I also set "Lines" to 0 and "Content Compression Resistance Priority" to 1000 (here fullscreen 1 and fullscreen 2):
Then I modify the code to use bigger width and height:
tile.frame = CGRectMake(10 + arc4random_uniform(100), 10 + arc4random_uniform(100), 2 * kWidth, 2 * kHeight);
Here is the result:
Both the background image
and the letter
label seem to have grown as intended.
However the font size of the label hasn't grown.
I've searched around and I think that I need something like tile.letter.adjustsFontSizeToFitWidth = YES
. But at the same time I can not use it with "Auto Layout" being on...
So my question is if there is any option in the "Interface Builder" available to make the font size grow as well?
UPDATE:
I've tried ismailgulek's suggestion to set the font size to 200 in Interface Builder and in the code tile.letter.adjustsFontSizeToFitWidth = YES
and it looks promising (here fullscreen):
but I have now the problem that I have set 40px as the letter.bottom
constraint in Interface Builder. But what if I need a bigger tile frame? Is there a way to use percentage instead of absolute pixel value in that constraint?
And another question is if it's possible to set the adjustsFontSizeToFitWidth
somewhere in the Interface Builder or do I have to use source code for that? I've tried adding a key to the "User Defined Runtime Attributes" - but then the app crashes at the runtime:
In general, the intrinsic content size simplifies the layout, reducing the number of constraints you need. However, using the intrinsic content size often requires setting the view's content-hugging and compression-resistance (CHCR) priorities, which can add additional complications.
Auto Layout dynamically calculates the size and position of all the views in your view hierarchy, based on constraints placed on those views. For example, you can constrain a button so that it is horizontally centered with an Image view and so that the button's top edge always remains 8 points below the image's bottom.
Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions.
A view that displays one or more lines of informational text.
adjustsFontSizeToFitWidth
property of UILabel
will not grow up the font size, it is only for reducing according to the documentation: Normally, the label text is drawn with the font
you specify in the font property. If this property is set to YES
, however, and the text in the text property exceeds the label’s bounding rectangle, the receiver starts reducing the font size until the string fits or the minimum font size is reached.
But if you set font size big enough (say 200) and set adjustsFontSizeToFitWidth
to YES
, i think you would get interestingly valuable results.
And do not forget to set Baseline
as Align Centers
, otherwise your text may not be seen properly.
Please inform us about your results.
For those who come here by the title and need a simple solution (after hours of investigations):
Problem example: If you use auto layout and set the UILabel height to be proportional to the screen height, you will get a frame which is low for iPhone (landscape) and high for iPad. How to adjust font height automatically?
Solution: in Interface Builder,
This will cause the font to get increased automatically to the current height.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With