EDIT: UITextView Label is Cut in Half (Horizontally) - iphone

I need help with this peculiar problem. I have a multiple choice question app and I have the choices as UITextview. Sometimes, choice D gets cut in half for whatever reason.
Screenshot:
Not sure what's going on here. I basically have the UITextView frame adjust to its contentSize.
CGRect dFrame = choiceD.frame;
dFrame.size.height = choiceD.contentSize.height;
choiceD.frame = dFrame;
Any ideas? Thanks in advance.

Caculate the size of string:
NSString *choiceDString = #"Equal the present value....";
CGSize size = [choiceDString sizeWithFont:[UIFont systemFontOfSize:CHOICE_FONT_SIZE] constrainedToSize:CGSizeMake(CHOICE_WIDTH, 100000)];
Init a label to content the choice string:
UILabel *choiceDLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,size.width,size.height)];
choiceDLabel.text= choiceDString;
Add the subview label for button:
[button addSubview:choiceLabel];

Use this code..Yo have define height of label according to your text length...
NSString *summary;
summary = #" your text";
CGSize sizeofbuttonorlable = [summary sizeWithFont:[UIFont systemFontOfSize:30]
constrainedToSize:CGSizeMake(self.view.bounds.size.width - 40, MAXFLOAT)
lineBreakMode:UILineBreakModeWordWrap];
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, sizeofbuttonorlable.height);
UILabel *choiceDLabel = [[UILabel alloc] initWithFrame:frame];
choiceDLabel.text= summary;
[button addSubview:choiceLabel];
Hope, this will help you...chill

My suggestion is to first Calculate the size of the text entered by you in the textView like:-
//Give the maximum size of label which that label can have.
CGSize maximumLabelSize = CGSizeMake(300,500);
CGSize expectedLabelSize = [Label.text sizeWithFont:Label.font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
//adjust the label the new height.
CGRect newDescFrame = Label.frame;
newLabelFrame.size.height = expectedLabelSize.height;
NSLog(#"%f",newLabelFrame.size.height);
//adjust the label the new width.
newLabelFrame.size.width = expectedLabelSize.width;
NSLog(#"%f",newLabelFrame.size.width);
//Set the label size according to the new height and width.
label.frame = newLabelFrame;
Write above mention code after entering the text in the textView.
Hope it helps.Thanks :)

Related

iPhone - Adjust UILabel width according to the text

How can I adjust the label Width according to the text? If text length is small I want the label width small...If text length is small I want the label width according to that text length. Is it possible?
Actually I have Two UIlabels. I need to place these two nearby. But if the first label's text is too small there will be a big gap. I want to remove this gap.
//use this for custom font
CGFloat width = [label.text sizeWithFont:[UIFont fontWithName:#"ChaparralPro-Bold" size:40 ]].width;
//use this for system font
CGFloat width = [label.text sizeWithFont:[UIFont systemFontOfSize:40 ]].width;
label.frame = CGRectMake(point.x, point.y, width,height);
//point.x, point.y -> origin for label;
//height -> your label height;
Function sizeWithFont: is deprecated in iOS 7.0, so you have to use sizeWithAttributes: for iOS 7.0+. Also to suport older versions, this code below can be used:
CGFloat width;
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0)
{
width = [text sizeWithFont:[UIFont fontWithName:#"Helvetica" size:16.0 ]].width;
}
else
{
width = ceil([text sizeWithAttributes:#{NSFontAttributeName: [UIFont fontWithName:#"Helvetica" size:16.0]}].width);
}
Using function ceil() on result of sizeWithAttributes: is recommended by Apple documentation:
"This method returns fractional sizes; to use a returned size to size views, you must raise its value to the nearest higher integer using the ceil function."
sizeWithAttributes
// In swift 2.0
let lblDescription = UILabel(frame: CGRectMake(0, 0, 200, 20))
lblDescription.numberOfLines = 0
lblDescription.text = "Sample text to show its whatever may be"
lblDescription.sizeToFit()
// Its automatically Adjust the height
Try these options,
UIFont *myFont = [UIFont boldSystemFontOfSize:15.0];
// Get the width of a string ...
CGSize size = [#"Some string here" sizeWithFont:myFont];
// Get the width of a string when wrapping within a particular width
NSString *mystring = #"some strings some string some strings...";
CGSize size = [mystring sizeWithFont:myFont
forWidth:150.0
lineBreakMode:UILineBreakModeWordWrap];
You can also try with [label sizeToFit]; Using this method, you can set frame of two labels as,
[firstLabel sizeToFit];
[secondLabel sizeToFit];
secondLabel.frame = CGRectMake(CGRectGetMaxX(firstLabel.frame), secondLabel.origin.y, secondLabel.frame.size.width, secondLabel.frame.size.height);
sizeWithFont constrainedToSize:lineBreakMode: is the original method to use. Here is an example of how to use it is below:
//Calculate the expected size based on the font and linebreak mode of your label
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;
just use to if you using constrain in your view or xib or cell
[LBl sizeToFit];
if its not working then
dispatch_async(dispatch_get_main_queue(), ^{
[LBl sizeToFit];
});
Try the following:
/* Consider these two labels as the labels that you use,
and that these labels have been initialized */
UILabel* firstLabel;
UILabel* secondLabel;
CGSize labelSize = [firstLabel.text sizeWithFont:[UIFont systemFontOfSize:12]];
//change the font size, or font as per your requirements
CGRect firstLabelRect = firstLabel.frame;
firstLabelRect.size.width = labelSize.width;
//You will get the width as per the text in label
firstLabel.frame = firstLabelRect;
/* Now, let's change the frame for the second label */
CGRect secondLabelRect;
CGFloat x = firstLabelRect.origin.x;
CGFloat y = firstLabelRect.origin.y;
x = x + labelSize.width + 20; //There are some changes here.
secondLabelRect = secondLabel.frame;
secondLabelRect.origin.x = x;
secondLabelRect.origin.y = y;
secondLabel.frame = secondLabelRect;

UILabel size to fit

I have a problem involving UILabel's sizeToFit method:
UILabel *questionLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,320,320)];
questionLabel.lineBreakMode = UILineBreakModeWordWrap;
questionLabel.backgroundColor=[UIColor clearColor];
questionLabel.textAlignment=UITextAlignmentLeft;
questionLabel.textColor=[UIColor blackColor];
questionLabel.tag=1;
questionLabel.font=[UIFont systemFontOfSize:13];
questionLabel.numberOfLines = 0;
[questionLabel sizeToFit];
[myView addSubview:questionLabel];
I had written this code for displaying my data. But if I write: [questionLabel sizeToFit] my data does not display properly. If I remove [questionLabel sizeToFit] then it is displaying but it only shows half the data.
Thanks and Regards.
NSString *yourString = #"write your label text here";
CGSize s = [yourString sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
questionLabel.frame = CGRectMake(0, 0, s.width, s.height);
Check if it helps.
I think it's best to use taus-iDeveloper answer to compute the size of a label.
I just want to say that the reason your code is not working is because you didn't set text to your UILabel so sizeToFit returns CGSizeZero (so it doesn't appear on screen). You have to set text before using sizeToFit.
I found that if AutoLayout is on then size to fit not work
i googled d above problem and came across some info that sizeToFit seems to be a bug and it has been reported to apple already.
So as a workaround u can use this code:
NSString * myText = [NSString stringWithString:#"some text"];
CGFloat constrainedSize = 265.0f;
UIFont * myFont = [UIFont fontWithName:#"Arial" size:19];
CGSize textSize = [myText sizeWithFont: myFont
constrainedToSize:CGSizeMake(constrainedSize, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
CGRect labelFrame = CGRectMake (0, 0, textSize.width, textSize.height);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
[label setFont:myFont];
[label setText:myText];
I've found that when using sizeToFit with a UILabel, in InterfaceBuilder you need to change the Autoshrink property from 'Fixed Font Size' to 'Minimum Font Size'. I usually then set its value to 0.5 to be sure its working properly.
just make sure you increase the number of lines of the label. Instead of
questionLable.numberOfLines = 0; make use of
questionLable.numberOfLines = 4;
As it will force the system to compute the smallest width possible for 4 lines.
You can achieve this via the Xib file too..

UIScrollView and UITextView autosizing possible?

I have a view that is pushed onto the Screen via navigation controllers:
Inside is a UIScrollView.
Then inside the UIScrollView are a few static objects like Images and Labels.
Then comes the hard bit, There is a UITextView with its text loaded from different text files of varying length.
I need to be able to have the UITextView size dynamically to its contents, and the same for the UIScrollView. Is this possible?
float length = [yourText length];
textview.frame = CGRectMake(44, 87, 923, ceilf(length/142)*25);
Here 25 is the constant value assumed as text font width. From this you can set scrollview frame reference to the textview frame.
You can do that with the help of following code. I had done that code for Label and same way you can do that with the help of text-field.
NSString *cellText = "Text Of Your Text-Field";
UIFont *cellFont = [UIFont fontWithName:#"Your Font Name" size:FONT_SIZE];//UIFont *cellFont = [UIFont fontWithName:#"Helvetica-Bold" size:13.0];
CGSize constraintSize = CGSizeMake(#"Width Of Your Text-Field", MAXFLOAT);//CGSize constraintSize = CGSizeMake(220.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
int height = labelSize.height;
frame.origin.x = Starting Position of X;
frame.origin.y = Starting Position of Y;
frame.size.width = Width Of Your TextField;
frame.size.height = height;
UILabel *lblName = [[[UILabel alloc] initWithFrame:frame] autorelease];
lblName.numberOfLines = 0;
lblName.lineBreakMode = UILineBreakModeWordWrap;
lblName.textAlignment = UITextAlignmentLeft;
lblName.textColor = [UIColor whiteColor];
lblName.backgroundColor = [UIColor clearColor];
lblName.font = [UIFont boldSystemFontOfSize:13.0];
And same way you can do For the Scrollview. Just you have to set the Frame of that scrollview and you are done.
Yes you could do something like this:
if ([textView length] > int//any number you want) {
textView.frame = CGRectMake(//just adjust the size an position here);
scrollView.contentSize = CGSizeMake(//adjust scrollView size)
}
else if ([textView length] > int//just another number) {
// you can continue looping that for how often you want
}
For the int in the if statement you check how long the text is. Based on that you adjust the size of both the scrollView and textView.

iOS: UILabel dynamic height using sizeWithFont:constrainedToSize:lineBreakMode: not working

I am trying to give my UILabel dynamic height so that my layout of other labels looks correct in both landscape and portrait.
In portrait, my text wraps to the second line, in landscape it does not. So, when using sizeWithFont:constrainedToSize:lineBreakMode: I get the same height when rotating both ways, when I had assumed it would be a larger number when the text was 2 lines.
How can I get the height of my UILabel when it has two lines of text or more (portrait) and get the new height which is one line, when in landscape?
I guess I am not understanding how to get dynamic height working...
UILabel *itemTitle = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, top, screen.size.width - 20, 200.0f)];
itemTitle.text = self.newsAsset.title;
itemTitle.adjustsFontSizeToFitWidth = NO;
itemTitle.autoresizingMask = UIViewAutoresizingFlexibleWidth;
itemTitle.font = [UIFont boldSystemFontOfSize:18.0];
itemTitle.textColor = [UIColor blackColor];
itemTitle.shadowColor = [UIColor whiteColor];
itemTitle.shadowOffset = CGSizeMake(0, 1);
itemTitle.backgroundColor = [UIColor blueColor];
itemTitle.lineBreakMode = UILineBreakModeWordWrap;
itemTitle.numberOfLines = 0;
[itemTitle sizeToFit];
// Set the height
CGSize maximumLabelSize = CGSizeMake(300,9999);
CGSize titleSize = [itemTitle.text sizeWithFont:itemTitle.font constrainedToSize:maximumLabelSize lineBreakMode:itemTitle.lineBreakMode];
NSLog(#"Height: %.f Width: %.f", titleSize.height, titleSize.width);
//Adjust the label the the new height
CGRect newFrame = itemTitle.frame;
newFrame.size.height = titleSize.height;
itemTitle.frame = newFrame;
// Add them!
[headerView addSubview:itemTitle];
[itemTitle release];
top += titleSize.height;
change the line where you set maximumLabelSize to
CGSize maximumLabelSize = CGSizeMake(headerView.bounds.size.width, CGFLOAT_MAX);
In your code as it is now, in either orientation you will get the same width and height, since you always pass a width of 300 to the sizeWithFont method. If you make it dynamic, maybe the result of the sizeWithFont will also change dynamically.

How do I fix a multi-line UILabel that is overflowing its containing UITableViewCell?

I'm having trouble displaying a multi-line UILabel in a custom UITableView cell.
I'm currently using this to calculate both the height of the cell...
NSString *cellText = [howtoSection objectAtIndex:row];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;
...and this is for the label itself.
// Calc the height
NSString *cellText = [howtoSection objectAtIndex:row];
CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cell.textLabel.font constrainedToSize:constraintSize lineBreakMode:cell.textLabel.lineBreakMode];
// Create the label frame
CGRect newFrame = cell.contentLabel.frame;
newFrame.size.height = labelSize.height;
cell.contentLabel.frame = newFrame;
[cell.contentLabel setText:[howtoSection objectAtIndex:row]];
Everything is working as planned except that the label is being pushed down and out of its cell. If it wasn't for this apparent top margin everything would fit.
Here's a link to a picture of what I'm seeing in the simulator...
iPhone Rendering Bug
Any help here would be greatly appreciated.
I don't see you setting cell.contentLabel.font in the code you show.
Also the label size calculation uses cell.textLabel.font to calculate its size but renders using the contentLabel variable.
Is it possible you're rendering with a different font than the calculation?
How are you adding your label to the contentView? It looks like the original positioning is wrong, since the height looks to be calculated correctly. If you comment out assigning the new frame, is the label in the correct position? My wager is that it isn't.