UILabel don't adjust in height to fit the text - ios

I know there is a lot of post about UILabel and it's size but i was unable to find a answer for my problem.
I have some UILabels with a text on 1 line, no more and don't want more lines, and the text is set to a minimum font scale to adjust. In portrait mode, the screen has enough space in heightand don't scale my UILabel in height to a critical point.
In landscape mode, i'm short in height and the UILabel is very small in height and the text is cut on the top and bottom.
I have tested everyting i have read like:
set line breaks to clip mode
use adjustsFontSizeToFitWidth = YES;
In every case, the text fit only in width but don't care about height.
Should i use some ugly hack to calcul the font size depending on the label height myself or have i miss something ?

CGRect labelFrame = CGRectMake(22, 50, 280, 150);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
[myLabel setText:finalRecipe];
[myLabel setBackgroundColor: [UIColor lightGrayColor]];
[myLabel setNumberOfLines:0];
CGFloat fontSize = 0.0f;
labelFrame.size = [myLabel.text sizeWithFont:myLabel.font
minFontSize:myLabel.minimumFontSize
actualFontSize:&fontSize
forWidth:labelFrame.width
lineBreakMode:myLabel.lineBreakMode];
myLabel.frame = labelFrame;
[self.view addSubview:myLabel];

Related

UIScrollView not working like most scroll views

I am trying to make my scroll view get to the bottom of my text without having to 'force scroll'. So when I scroll down, the scroll bar at the right stops before I reach the bottom portion of the remaining text. I have to then scroll again with force (so that the scroll bar at the right shrinks, revealing the remaining text). Hopefully someone has encountered this. I don't want to force scroll. I want it all displayed in a clean way.
UIScrollView *theScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 568.0f)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(17.0f, 150.0f, 286.0f, 568.0f)];
label.text = #" a very large portion of text ";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont fontWithName:#"Chalkduster" size:16.0];
label.lineBreakMode = NSLineBreakByWordWrapping;
label.numberOfLines = 0;
[label sizeToFit];
[theScroll setShowsVerticalScrollIndicator:NO];
theScroll.contentSize = CGSizeMake(theScroll.contentSize.width, label.frame.size.height);
[theScroll addSubview:label];
[self.view addSubview:theScroll];
This is all in the viewDidLoad method.
Two things don't quite seem right.
You set the label's origin.y to 150. Did you mean to have the label start so far from the top of the scroll view's content area?
You set the scroll view's contentSize.height to be the same as the label's height. This conflicts with #1. The contentSize.height should be tall enough for all of the content.
Assuming #1 is correct, #2 should be set as:
theScroll.contentSize = CGSizeMake(theScroll.contentSize.width, CGRectGetMaxY(label.frame));
Remember, the bottom of the label will be its origin + its height, not just its height.

Width of UILabel created programmatically

I'm trying to create a UILabel programmatically that fits it's content.
The best I could do was to use the NSString's method sizeWithAtributes to get it's content's required width and then apply a constraint that fixes the width to that value.
Is this the way it is supposed to be done using autolayout?
if you need some with aoutolayouts, i try comment:
UILabel *testLabel = [UILabel new];
[testLabel setFont:[UIFont systemFontOfSize:16]];
[testLabel setText:#"Test text for size context"];
[testLabel setTextAlignment:NSTextAlignmentLeft];
[testLabel setNumberOfLines:0]; //0 - infiniy lines if not enough space more
[testLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal]; // Autolayout rule: The higher the priority, the more important hold the text.. by horizontal
[testLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];//some like above but verticale
[testLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];// reverse like Hugging but Compression. then lower priority then text croping.
[testLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisVertical];
[testLabel preferredMaxLayoutWidth:200];//Start new line each 200 points width
[testLabel setPreferredMaxLayoutWidth:YES];// resize Font size by label size.
[testLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; // Disable autolaresize, enable autolayouts
At First read the best answer for any question about UILabel
And also by using following code you can create dynamic size of UILabel
UILabel *myLabelName = [[UILabel alloc] init];
myLabelName.backgroundColor = [UIColor redColor];
[myLabelName setFont: [UIFont fontWithName:#"OpenSans" size:13]]; // set font and it's size as per your requirement.
myLabelName.textAlignment = NSTextAlignmentLeft;
myLabelName.frame = CGRectMake(lblComplainTitle.frame.origin.x + lblComplainTitle.frame.size.width + 7, lblComplainTitle.frame.origin.y +2, 160, 20);
myLabelName.text = #"This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text, This is my testing text.";
myLabelName.textColor = [UIColor blackColor];
[self setDynamicHeightOfLabel:myLabelName withLblWidth:160 andFontSize:13]; // here in this function you need to pass object of label with width (as you wabt) and font size
[self.view addSubview:myLabelName];
Code of function, name is setDynamicHeightOfLabel:withLblWidth:andFontSize
-(void) setDynamicHeightOfLabel:(UILabel *) myLabel withLblWidth:(CGFloat) width andFontSize:(int) fontSize
{
CGSize myLabelSize = CGSizeMake(width, FLT_MAX);
CGSize expecteingmyLabelSize = [myLabel.text sizeWithFont:myLabel.font constrainedToSize:myLabelSize lineBreakMode:myLabel.lineBreakMode];
CGRect lblFrame = myLabel.frame;
lblFrame.size.height = expecteingmyLabelSize.height;
myLabel.frame = lblFrame;
int addressLine = myLabel.frame.size.height/fontSize;
myLabel.numberOfLines = addressLine;
}
I create function because you can create any label with dynamic size without use of repeated code.
There is a couple of steps that you have to do to achieve this using autolayout.
Set layout constrains for the label.
Set height constraint with low priority.
Set numberOfLines to 0 to allow multiline text.
Set preferredMaxLayoutWidth for the label.
The preferredMaxLayoutWidth is used by label to calculate its height.
This property affects the size of the label when layout constraints
are applied to it. During layout, if the text extends beyond the width
specified by this property, the additional text is flowed to one or
more new lines, thereby increasing the height of the label.
Hi you can manage the width of lable by doing so programatically,
CGRect frame;
frame =self.yourLabel.frame;
frame.size.width +=20;
self.yourLabel.frame=frame;
Thanks

Resize text in a UILabel (subview of UITableViewCell) to fit width

I have a UITableViewCell that is customized, sometime, text in UILabel (UILabel is subview in UITableViewCell) is exceeds size, and it get some dots (like ...) in label.
I want to adjust font size to fit width. But it does not work.
Here is my code
In Class implement for UITableviewCell
-(void) awakeFromNib{
myLabel.minimumFontSize = 8;
myLabel.adjustsFontSizeToFitWidth = YES;
}
Please help me!!!
Try this simple code
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
label.backgroundColor=[UIColor clearColor];
label.text= yourString;
label.numberOfLines=0;
label.adjustsFontSizeToFitWidth=YES;
label.minimumFontSize=6;
label.textAlignment=UITextAlignmentCenter;
[self.View addSubview:label];
CGFloat fontSize = 20;
while (fontSize > 0.0)
{
CGSize size = [yourString sizeWithFont:[UIFont fontWithName:#"Rockwell-Bold" size:fontSize] constrainedToSize:CGSizeMake(label.frame.size.width, 10000) lineBreakMode:UILineBreakModeWordWrap];
if (size.height <= label.frame.size.height) break;
fontSize -= 1.0;
}
//set font size
label.font = [UIFont fontWithName:#"Rockwell-Bold" size:fontSize];
[label release];
try this one...
myLabel.numberOfLines=0;
[myLabel sizeToFit];
at First. In your TableViewClass, in heightForRowAtIndexPath you have to calculate the height of your text according to your label and return it.
and make your label's autoLayout property flexible height. You do not need to reset your label's frame. try this it will work....

UILabel adjust its frame to text length

Is it possible that my UILabel adjusts its frame size to the length of the text of the UILabel?? If yes, how?
Thanks.
Use -sizeToFit:
[label sizeToFit];
And what's more, you can also adjust the font size to fit width:
[label setAdjustsFontSizeToFitWidth:YES];
[label setMinimumFontSize:8.f];

Positioning UILabel inside a UITextView

Trying to position a UILabel above some text, but the elements are "stepping" on each other. I positioned them with x,y,h,w such that they should not do this but alas, they are. You can see in the screenshot that the label text "Abalone" and the text are commingled. What changes do I need to make here? I've already tried changing the x,y,w,h values to no great effect...
txtView = [[UITextView alloc] initWithFrame:CGRectMake(10,20,280,300)];
[txtView setFont:[UIFont fontWithName:#"ArialMT" size:14]];
txtView.text = word.definition;
txtView.editable = NO;
txtView.scrollEnabled = YES;
[txtView scrollRangeToVisible:NSMakeRange([txtView.text length], 0)];
txtView.minimumZoomScale = 1;
txtView.maximumZoomScale = 10;
UILabel *wordTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 250, 20)];
wordTitle.text = word.term;
[txtView addSubview:wordTitle];
[detailsViewController.view addSubview:txtView];
I think the problem is you are adding the label view as a subview to UITextview. You have to reserve some space for the label view in the UITextview. You can either add some padding to the textView, which can be confusing, because padding in UITextView is not constant and varies based on the font size. Or you can add your UILabel directly to the detailsViewController.view and start your UITextView below the label.
Hope it helps.

Resources