iOS 7 Autolayout does not correctly resize UILabel when font size is large - ios7

I used the solution here (UILabel sizeToFit doesn't work with autolayout ios6) to get the UILabel to fit the text.
However, when I increased my font size, the height did not fit any more.
I am not familiar with typography, but I guess the padding within the bounding box of the text always exists, and it is just not obvious when font size is small.
In my project, the text in the UILabel switches between two fonts, and the difference of padding makes it so hard to align it with other controls on the same view.
So my question is, how can I get rid of the padding-top and padding-bottom in a UILabel with large font-size?

Simply, you can't.
But you can decrease the Height accordingly as font increases using following formula,
Int heightToDecrease = FontSize - 12; //Change the value to fit your font family.
lblText.frame = CGRectMake(lblText.frame.origion.x, lblText.frame.origion.y, lblText.frame.size.width, lblText.frame.size.height - heightToDecrease);
I know this is not the perfect solution but it will help you for sure.

Related

Objective C multiline UILabel is not working

Here I have attached the scenario of my issue.
Current View
Here Top Title is a bit long string. But my view is shown partially.
Here I added the storyboard properties that I used for my UILabel.
Can someone suggest a solution to make this UiLabel as multilined one ?
You can't have a multi-line label with a fixed font size, if that label is constrained to a certain height.
Either the height needs to scale to allow more lines at that fixed font size, or the font size needs to scale down to fit all the text in.
If neither are possible, then the text will be truncated.
If your label is constrained to a certain height, then you can change the Autoshrink setting from Fixed font size to Minimum font size and specify the smallest size that you will allow the font to shrink to in order to accommodate longer strings.
Try to change the relation of constraint height: Greater than or equal

How to Know the maximum font size allowed for a given font name?

Is there any way to know the maximum size a font allowed.
I am doing
textInputTextView.font = [UIFont fontWithName:currentFontName size:doubleFontSize];
frame.size = textInputTextView.contentSize;
For different fonts I am getting wrong contentSize when doubleFontSize is more than 70. For some fonts I get wrong contentSize when doubleFontSize variable is only 40.
I am guessing textView.font = doubleSizeFont is too big for those particular fonts. All are ok when font size is small between 10 to 30.
Is there any way to know the maximum size allowed by a particular font?
Details:
I need an image from UITextView. I am using UIGraphicsGetImageFromCurrentImageContext image is producing ok. But I realized image quality is low. So I tried to make the TextView frame and font double.
When I Write in big font "I am a good boy" if good is the right most word. I get "I am a boy" the right most word good disappears I get some blank space on right side of the image. How tragic :)
You Can Calculate height/Width of the content by using the following code.Then resize your frame. Here my Width is fixed (290) and Calculating the height.
CGSize strSize= [DataToBeDisplayed sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:CGSizeMake(290.0,99999.0)];
frame.size.height = strSize.height
I believe the issue that you are having is because you are using an UITextView. Text views inherits from the UIScrollView so it would not decrease the size of your font, it would only adapt its contents size and make your text scrollable.
Use an UITextField instead which also inherits from the UIView (thus can be used as an image) but allows for greater control. Fix its size to the maximum you would like it to be on screen and then working with both the [mylabel adjustsFontSizeToFitWidth:Y/N] and the [mylabel setminimumfontsize:somefontsize] methods, constrain it if too much text is entered. This will yield you a crisp big font and dynamically fit it upon input
~/End of Line

unable to change size of font in UILabel

I am instantiating a UILabel programatically. The label displays numbers in a count down. The problem is I seem unable to increase the font size dispute the fact I have the following code
[self.viewAnimation addSubview:lblCounter];
[lblCounter setAdjustsFontSizeToFitWidth:NO];
[lblCounter setMinimumFontSize:30.0];
[lblCounter setTextAlignment:UITextAlignmentCenter];
The UILabel is plenty big enough to fit size 30 font.
Anyone had similar problems?
Use [lblCounter setFont:[UIFont systemFontOfSize:30]], minimum font size only applies when adjustsFontSizeToFitWidth is YES, and regulates how much the font will shrink before cropping takes place.

UILabel with no whitespace above or below text

I have this UILabel in an app where the bottom of the text needs to (apppear to) rest on the edge of another uiview. The label also gets scaled by arbitrary amounts. The problem is that applying a scale to the label also scales the whitespace below the text in the label. So for instance scaling by 2.0f makes the whitespace twice as big, pushing the text farther away from the edge.
Is there a simple way of making a label perfectly fit the text's size so that the bottom pixel of the text is at the very bottom of the label view?
Let me present you, the most useful method for these situations:
[myString sizeWithFont: ....];
This method (and its multiple variations) return the size that a NSString instance will use, therefore, you can scale the UILabel to your needs. iOS lacks a "Vertical Alignment" option.
Here is a similar stackoverflow question, if you're still in doubt.

NSString sizeWithAttributes: Inaccuracy

I want to know the width of an NSString displayed on the screen in pixels. So I can fit an NSTextField its bounds to be exactly the length of the string itself. So I used IB's "Label" NSTextField and for those who don't know what I mean, I got a label with title "Label", font "Lucida Grande 13px", not selectable, not editable, regular size, no background and according to IB its width is 38px wide.
If I want to get its width programatically I use
[#"Label" sizeWithAttributes: [NSDictionary dictionaryWithObject: [NSFont fontWithName: #"Lucida Grande" size: 13] forKey: NSFontAttributeName]].width
Which will give me 33.293457 . So that's about 5 px of the real width..
I believe what you are noticing is the difference between the frame of the control and it's layout frame. See Frame vs Layout Frame for a good explanation.
You are doing the right thing in computing the width. For a Label, there is no extra padding at the top or bottom of the control, which is why you saw no problems with the height of your control. However, on the left and right of the control, there are an additional three pixels. This can be verified by looking at the Frame and Layout of a Label control in IB.
So, the ~5 pixels you noticed is actually exactly 6. Once you take into account this padding, you should have no further trouble.
Unfortunately, there is no API to determine what the padding is for various controls (Push Buttons have an additional 6 pixels on each side). I would suggest filing a bug report at http://bugreport.apple.com - Apple does base what APIs they provide in part on the number of requests for them. While IB will tell you, you will need to code those values yourself. If they change in the next OS release, you will need to update your application.
That actually sounds reasonable. You are comparing two different widths; the width of a raw string vs. the width of a string contained in an NSTextField. The contained string likely has just a tad bit of padding on either end, among other minor differences.