Dynamically changing font size of text without changing UILabel size - ios7

I am developing an application in which I am having fixed size UILabel which may contain text of any size.
I need to resize font size without changing UILabel size.
Following is how I have tried to achieve this:
UILabel *errorMessageLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,50,300,30)];
errorMessageLabel.lineBreakMode =NSLineBreakByTruncatingTail;
errorMessageLabel.numberOfLines = 0;
errorMessageLabel.adjustsFontSizeToFitWidth=YES;
errorMessageLabel.adjustsLetterSpacingToFitWidth=YES;
if ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0) {
[errorMessageLabel setTextAlignment:NSTextAlignmentCenter];
}
else{
[errorMessageLabel setTextAlignment:UITextAlignmentCenter];
errorMessageLabel.minimumFontSize=5.0;
}
[errorMessageLabel setTag:405];
[errorMessageLabel setFont:[UIFont systemFontOfSize:13]];
[backgroundView addSubview:errorMessageLabel];
Above solution works for iOS 7 but when I try to run this on iOS 6 it changes font size properly but sets text alignment to left ignoring its centre alignment.
Can any one please let me know how I can achieve this? I tried to search form other solution but nothing is working. I cannot change UILabel's size

errorMessageLabel.numberOfLines = 1;
errorMessageLabel.adjustsFontSizeToFitWidth = YES;
errorMessageLabel.minimumFontSize = MIN_FONT_SIZE;

Related

UITextView text is missing when adjust frame according to text in iOS7?

Hi I am trying to set UITextView height as per content. Its working fine for adjusting frame of UITextView.
But coming to text is missing as like below image shown. This happn only in iOS7 . Its displaying well in iOS6. If i tap on textview i started to edit the textview complete text is dispalying.
I wrote the fllowing lines of code` notesField.text = [editWellObject objectForKey:#"Notes"];
// Adjusting Frame as per text
CGSize size = [notesField sizeThatFits:CGSizeMake(notesField.frame.size.width, FLT_MAX)];
CGRect frame = notesField.frame;
float difference = size.height - frame.size.height;
frame.size.height = size.height;
notesField.frame = frame;
Please help me in this issue. Thanks in Advance.
Few days back I was struggling with the same issue and I have found solution with resetting property scrollEnable from NO to YES. Following is my code snippet
internalTextView.scrollEnabled = NO;
if (newSizeH >= maxHeight)
internalTextView.scrollEnabled = YES;

How do you vertically align UILabel text to the top now that sizeWithFront is deprecated? ios7

It's been EXTREMELY frustrating trying to vertically align UILabel text to the top. I've search google for hours, and everything I've found has been deprecated.
What is the best way to do this? I've used numberOfLines=0 & sizeToFit... but this doesn't work upon initial loading of the UILabel on a custom TVCell.
Does anyone have a code snippet that works for align UILabel text to the top on a custom cell?
Calculate height of text and set UILabel frame again.
try this-
CGSize maxSize = CGSizeMake(label.frame.size.width, label.frame.size.height);
CGSize labelSize = [label.text boundingRectWithSize:maxSize
options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin
attributes:#{NSFontAttributeName:label.font}
context:nil].size;
[label setFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y, label.frame.size.width, labelSize.height)];

UIlabel truncating text after linebreaks in iOS7 only

I am trying to display a UIlabel with a text having a linebreak. It works fine on iOS6 but on iOS7 , it truncates the text after linebreak is encountered.
I have kept
label.numberOfLines = 1;
The UIlabel frame which I am setting is also correct. I have cross checked it.
Does anyone has any other pointers to this problem?
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
This will allow the label to dynamically assign as many lines into the label as will fit into the label.frame.size.height of the label based on the current font.

NSAttributedString boundingRectWithSize gives different heights between iOS 6 and iOS 7

Recently updated my App to iOS 7 using XCode 5 and found that boundingRectWithSize gives different heights (in the size part) calculating the bounds of attributed Strings.
The following line gives me different Results between iOS 6 and iOS 7:
CGRect rect = [self boundingRectWithSize:CGSizeMake(inWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil];
"self" is an NSAttributedString and "inWidth" is the maximum width in pixels the string should fit in.
I think thats because iOS 7 has a different font handling than iOS 6.
Anyone got a working solution to calculate the height of a string on both iOS versions?
As we cant use sizeWithAttributes for all iOS greater than 4.3 we have to write conditional code for 7.0 and previous iOS. So I suggest to use given solution
UILabel *gettingSizeLabel = [[UILabel alloc] init];
gettingSizeLabel.font = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16];
gettingSizeLabel.text = #"YOUR TEXT HERE";
gettingSizeLabel.numberOfLines = 0;
CGSize maximumLabelSize = CGSizeMake(310, 9999); // this width will be as per your requirement
CGSize expectedSize = [gettingSizeLabel sizeThatFits:maximumLabelSize];
The option is quite well and working smoothly in all iOS without conditional code.
I had the same problem, for me a simple ceil() on the height solved it.
Also be sure to set the right attributes for youre attributed string e.g.
#{NSParagraphStyleAttributeName: paragraphStyle, NSFontAttributeName : label.font}

ios6 uilabel not center text aligned

I'm working on a ios app minimum ios 5 and i have just hit a weird issue with uilabels. or maybe im missing something obvious. anyway the problem im having is that i have a uilabel which is to be centered text aligned. all works fine on ios 5 but on ios 6 it is always is left aligned. I seen that the old way of doing uilabel text align has deprecated and that setting it as so should work.
self.topComment.textAlignment = NSTextAlignmentCenter;
But no even this way it still only is center aligned on ios 5 and left aligned on ios 6.
I do have some code that resizes the font of the text in the label to try make it fit with a min and max size.
UIFont *font = self.topComment.font;
for(int i = maxFont; i > minFont; i--)
{
// Set the new font size.
font = [font fontWithSize:i];
CGSize constraintSize = CGSizeMake(self.topComment.frame.size.width, 1000);
CGSize labelSize = [topString sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
if(labelSize.height <= self.topComment.frame.size.height ) {
fits = YES;
break;
}
//self.topComment.text = topString;
}
self.topComment.font = font;
self.topComment.text = topString;
So that is the only thing i am doing to the label but it always is left aligned in ios 6. Important to note that if i drop in a uilabel with text and center align it and dont use the above code then it is centered on both ios 5 and 6.
Ok looks like turning on Tighten Letter Spacing has a different result on ios 5 and 6. I honestly cant explain why the difference but just turning this off gives the desired centering of labels for me. Just leaving this here as an answer in case anyone else makes the same silly mistake.
For example:
lbl.adjustsLetterSpacingToFitWidth = NO;
Note this is deprecated in iOS 7.0.
For iOS 6 or later:
label.textAlignment = NSTextAlignmentLeft;