Replacement for minimumFontSize for a UILabel - ios

I'm aware of minimumScaleFactor but it isn't useful for the result I'm after.
I have two UILabels and I want them both to be the same size. I want them to fit to the label's view if possible down to a minimum size and then I'll do a check to set them both to the smallest font size, so that they both fit and are both the same size.
I don't think I can achieve this using minimumScaleFactor because it doesn't seem to actually change the font size, it seems to use some sort of scaling on the view (correct me if I'm wrong). I don't seem to be able to set this scale manually other than setting it's minimum value so it seems to be useless in my circumstances.

Set max_msg_height that you want in your cell. This function will check if your message length is more than max_mgs_height and font size and if it is greater than 12 points, it will continue. In my case i have set max_msg_height = 160. Default font size to 16. So, if I got message which is not adjust in lable frame then I reduce font by 1 point by using this while loop as shown below:
while (messageLbl.frame.size.height > max_msg_height &&
messageLbl.font.pointSize>12) {
messageLbl.font = [UIFont systemFontOfSize:messageLbl.font.pointSize-1];
[messageLbl sizeToFit];
}

Related

How to use biggest font in UILabel as possible?

In my case UILabel size can change dynamical . If it is bigger I want to have the best fitting, biggest font size. Is it possible to set it in Storyboard or I need calculate the best one programmatically?
If you have more than 1 word in the label, you can set a very high font size and, set AutoShrink -> Minimum Font Size to a small size. Then in run time app will auto shrink to maximum possible font size. But this doesn't work for if you only have a single word.

iOS UILabel bounding rectangle not sized correctly

I have a UILabel (as highlighted in yellow), which has the following conditions applied to it.
Label has variable text length
Font set to 40
Minimum font-size set to 20
Number of lines set to 3
Although this looks like a duplicated question I believe it is not. The issue I am having is that when the text exceeds the available 3 line length after being sized-down to 20 points, the UILabel's bounding box is sized incorrectly (i.e. note the extraneous spacing above and below the text).
The end result should be a UILabel without any spacing. Is there are solution to this, while keeping the number of lines set to 3?
That looks like a bug. If you increase your base font size, you will see the space increase. Also, if you inspect the layout at runtime, you will see the content size to be calculated as too big.
My guess is, UILabel takes your original font size (40) to calculate the content size for 3 lines of text and does not take into account that the font size has already been decreased before truncation.
I fiddled with content hugging/compression priorities but could not make it work either.
The only workaround I found was to manually set the font size down to 20. That will get you the frame you want.

Multi-line UILabel with auto layout, how to adjust font size according to content without changing label's frame?

I have a multi-line UILabel, which will display contents at run time, contents may only need 1 line or up to 3 lines.
When there is less contents, I'd like the label using a larger font size, and when there are more contents, I want the label to use multiple lines and smaller font size.
Due to autolayout, I can't set the frame of UILabel, and the frame of the label is unknown until run time, so that it can be compatible with different devices.
Currently I set line number of the label as a fixed number, which will divide contents into multiple lines when there is more contents, but when there is less contents, the single line uses a font size as small as when there is more contents. This is not the best experience.
set num of lines and font scale factor 0.5 or 0.75
If your content is small it would use the occupied lines and font size will be the actual size. When your content grows these properties will check if with actual fontsize the content can be adjusted to all lines or not. if its not adjustable with full size then font will be scalled to adjust in contentview.

UILabel text is not displaying properly

I want to show the text in uilabel. The text is ,000124520.061112782.,3299985343, I have assign line break mode to character wrap and number of lines to 0. But in label first line is displayed only the , and then other characters are displayed in next line. But i want to display the text in two lines only. But it is displayed in three lines. I don't know why , is displayed in first line. Please anyone help me.
You should set the numberOfLines of your UILabel to 1.
When the numberOfLines property of a UILabel is set to 0 the UILabel will render its text over as many lines as it needs - e.g. its infinite. To fix your text to a single line you should therefore set the property to 1.
Obviously your text may not all fit on a single line so you should also set the UILabel's minimumScaleFactor property to allow the UILabel to adjust its font size downward (to the supplied limit) to fit the content into the frame of the label. You will also need to set the UILabel's adjustsFontSizeToFitWidth property to YES.
Further to the above an easy way to calculate the value for the minimumScaleFactor property is to divide the minimum font size by the maximum font size. So, say for example your label's default font size was 12 and you were happy for it to drop to 10, then assign the minimum scale factor as follows:
myLabel.minimumScaleFactor = 10/12.0f;

Fonts not fitting properly in UILabel - Ascender or Descender cropped

I've done extensive searching/reading/testing and cannot find a solution to this problem.
I've tried since iOS 4.3 and it's still not resolved in iOS7.
The problem is this: Fonts at large sizes can have their Ascenders or Descenders cropped in a UILabel.
Here's a screenshot directly from the Xcode 5.1 UI (no code at all!) showing the problem - font size 300 points:
As you can see, even a simple font like Helvetica Neue (bold or not) has it's Descender cropped. (You're seeing UIViewController > UIView > UILabel)
If you try this and then change the point size you'll see the font scale down, and eventually the Descender will not be cropped. Here it is again at 160 points:
Notice also that some fonts do not get cropped and others do - try Noteworthy, or Papyrus, or Savoye LET - all of which are standard iOS & fonts....
I'm talking about Height here - I know I can use adjustsFontSizeToFitWidth=YES to see the entire length, and I also know I can use sizeToFit, however neither guarantees no cropping of the Ascender/Descender.
Notice also that calculating the height using Ascender/Descender values does not help as the main issue is that the font is not centered vertically within the label when it is drawn. (If it were, it would be a simple calculation.)
So here is the question: How can I show a font as tall as possible and be assured that the Ascender/Descender is not cropped regardless of the font used?
EDIT:
I re-read my question and realized I did not ask it properly - I'm able to resize the label to fit the font - that's not the problem. Here's the revised question:
How can I draw text in a UILabel as large as possible and be assured that it is centered vertically, with no cropping of the Ascender or Descender?
I can easily figure out the overall height of the text, and once I know it will fit, how can draw it in the UILabel vertically centered?
For Example: In the first screenshot, the text "Tg" is cropped, but it is easily short enough to fit vertically in the label. In fact, it could be even larger and still fit if it were properly centered. But I know of no way to center it vertically...
The size of the label can be sized according the length of the string, the font attribute used and the size of the font. I use this method a lot and works great for such requirements -
NSString *textWithinLabel = #"Whatever you like, passed from where ever";
CGSize maximumLabelSize = CGSizeMake(300, 1000); //Place your maximum sizes here
//Here I've used Helvetica, though you can pass any font name or font size here to try out
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[[UIFont fontWithName:#"Helvetica" size:15] forKey: NSFontAttributeName];
CGSize newExpectedLabelSize = [textWithinLabel boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;
CGRect frame = self.yourLabel.frame;
frame.size.height = newExpectedLabelSize.height;
self.yourLabel.frame = frame;
This example will change the height of the label, though you can use it to change width too etc.
The stringAttributes here are used to calculate the size, not to set the attributes. So for example, if your label is using 14pts and you calculate the height for 30pts, it won't change the height of the font, it will only increase the size of the label to accommodate the larger font size. If you want this method to also change the font attributes, you would need to add the appropriate code at the bottom of the method - self.yourLabel.text.font = ... etc.
I hope this answers your question,
Thanks, Jim.
I tried this and it solved my problem. Essentially, the height of the letter is Ascent+Descent. So that's all the space the label needs vertically.
1. [commentLabel sizeToFit]; //To trim out the unwanted area from the label
2. [commentLabel setFrame:CGRectMake(commentLabel.frame.origin.x, commentLabel.frame.origin.y + ABS(commentLabel.font.descender), commentLabel.frame.size.width, commentLabel.font.ascender + ABS(commentLabel.font.descender))];
//The frame adjustment in **(2)**moves the label down by commentLabel.font.descender because the label by default is aligned based on their actual bottom line instead of the actual line we use on notebooks, where the descender hangs down from the line. In case of a label the bottom line is the lower tip of the descender.

Resources