how to make UILabel dynamically without overlapping with other UILabel - iphone

I've tried to search online, but haven't found a clear answer for my issue so I've come to ask for your expert advice. I have a view with 2 labels on it. Both label will display different string length from the plist.
When i run the app, the label will overlapped with other label depending on the string length.
Below is the screenshot for my problem

You have to change your secondLabel origin.
CGRect frame = secondLabel.frame;
frame.origin.y= firstLabel.frame.origin.y + firstLabel.frame.size.height;
[secondLabel setFrame:frame];

Better option is to use UITextView instead of UILabel but if you still want to go with lable then
with the use of below code you can find the height of the text and can set your lable's frame according to that height
NSString *text = [arr objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(contentWidth - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:20] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
here contentWidth is the width of your label and CELL_CONTENT_MARGIN = 10;

You need to set the 'Y' of second label. Take the Height of first label text and then set it to the Second Label 'Y'.
Hope it'll help you.
CGSize LblSize=[[Label1 text] sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(320.0f, 400.0f)];
UILabel *Label2=[[UILabel alloc] initWithFrame:Label2Rect];
CGRect Label2Rect=[Label2 Frame];
Label2Rect.origin.y=LblSize.height+30.0f; //add some extra spaces, I have added 30.0f here
[Label2 setFrame:Label2Rect];

Ey, you can solve it in many ways.
For example, you can fill your first label with the dessired text and then call to
[label1 sizeToFit]
With that call, your label now has the proper size, adapted to the lenght of your text. Now you can just place your second label after your first one.
label2.frame = CGRectMake (x, label1.frame.size.height + ..., .....)
Hope it helps!

Related

Horizontal scrolling text?

Can anyone provide any tips when it comes to implementing some text that scrolls horizontally?
Right now I have a ScrollView *scrollView with a UILabel *textLabel inside of it.
I have the position of the label changing within the view until it is outside the bounds of the screen. The label is then reset to its original position and it starts scrolling again.
The problem(s) I am having are when I say: [textLabel sizeToFit];
This takes away the labels ability to handle multiple lines of text because once I say [textLabel sizeToFit]; it changes it into one big string.
Is there a simpler way to achieve the desired effect?
ANY help is greatly appreciated
P.S. The text inside of the label will be parsed from a website... so the size/length of the string will not be consistant.
try this
CGSize maximumLabelSize = CGSizeMake(widthOfLabel, 9999);
CGSize expectedLabelSize = [#"Text" sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];
then you can use expectedLabelSize.height or expectedLabelSize.width and change the frame size of UILabel

UITextView dynamically resizing text box / cell

I'm loading my UITextView from an XML feed so the text is constantly changing. I'm trying the following to resize the cell and text, and it resizes the cell but not the text view, it's just not displaying the text view, or sometimes just part of it.
Any tips along the right way will be really appreciated;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
AssessObject *newObj1;
newObj1=[totalArray objectAtIndex:indexPath.row];
NSString *cellText = newObj1.routeText;
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:16.0];
CGSize constraintSize = CGSizeMake(188.0, CGFLOAT_MAX);
CGSize textViewSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return textViewSize.height + 200;
}
Check the AutoresizingMask of the UITextView you have added to your cell.
Make sure it is set so that it resizes with the cell (you can do this either in IB, or via code using the UIViewAutoresizingMaskFlexibleWidth|UIViewAutoresizingMaskFlexibleWidth value)
Set the textView size equal to textView's contentSize.
Something like this:
CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, textView.contentSize.height);
I'm making the height of the textView equal to the height of it's contentView.
setup the font size, text content and frame rect of the UITextView, then [UITextView sizeToFit] to calculate the contentSize of UITextView, then calculate the row height with the size of contentSize.
Don't forget to resize the frame rect of UITextView;
I have used Bruno's idea to resize my TextView according to the amount of text, when I put it to the ScrollView. This is how I do this. A bunch of constants there, that you may not use. It is important to resize textView after adding it to the ScrollView.
// Programmatic creation of scroll view layout
NSString *text = #"Your text";
CGFloat textOffSetInColumn = 10;
CGFloat infoTextWidth = 196;
CGFloat infoOffsetVertical = 36;
CGFloat initialTextHeight = 50;
// Create textView with initial height
CGRect infoTextFrame = CGRectMake(textOffSetInColumn, infoOffsetVertical, infoTextWidth, initialTextHeight);
infoTextView = [[UITextView alloc] initWithFrame:infoTextFrame];
infoTextView.text = text;
[scrollView addSubview:infoTextView];
// Resize textView
CGFloat infoTextHeight = infoTextView.contentSize.height;
infoTextFrame = CGRectMake(textOffSetInColumn, infoOffsetVertical, infoTextWidth, infoTextHeight);
infoTextView.frame = infoTextFrame;
If you want to change the size of TextView and center it to the previous center, you can use this code:
// Changing size of TextView and centering
CGPoint center = self.categoryTextView.center;
self.categoryTextView.frame = CGRectMake(_categoryTextView.frame.origin.x, _categoryTextView.frame.origin.y, _categoryTextView.frame.size.width, _categoryTextView.contentSize.height);
self.categoryTextView.center = center;
Instead of categoryTextView use your own Outlet name.

Finding the number of lines in UITextView

I'm using UIScrollView in which I have placed a UIImageView and a UITextView. I make the UIScrollView to scroll both the images and text and it works fine, but my UITextView contains dynamic text (i.e number of lines is different for each time). So I can't find the way to assign contentSize of UITextView. Is there any way to do this?
This and this might help you.
You can put a condition that if width of the new CGSize is greater than textview width then number of lines = 2 else 1.
calculate the text width and define your text view width using below code
+(float) calculateHeightOfTextFromWidth:(NSString *) text: (UIFont *)withFont: (float)width
:(UILineBreakMode)lineBreakMode
{
[text retain];
[withFont retain];
CGSize suggestedSize = [text sizeWithFont:withFont constrainedToSize:CGSizeMake(215, 1000) lineBreakMode:lineBreakMode];
[text release];
[withFont release];
return suggestedSize.height;
}
and use when you want to display dynamic text as
float titleHeight;
titleHeight = [Your view controllre ViewController calculateHeightOfTextFromWidth:[NSString stringWithFormat:#"%#",[dict objectForKey:#"title"]] :[UIFont systemFontOfSize:14]:300 :UILineBreakModeTailTruncation];
give this titleHeight to your textview or do calculation dividation using titleheight ,you can get the number of lines
If you get problem then reply

UILabel alignment: show "..." at the end

In my app I am assigning fixed width and height to uilabel and assigning some text to it. My problem is if the text length is more than the label height I want to show "..." at the end of the text eg "apple... " . Is there is any property to show like this?
Yes, please check the line break property and set it to truncateTail
label.lineBrakMode = UILineBreakModeTailTruncation;
Hope this helps,
CGSize maximumSize = CGSizeMake(300, 40); //to keep height fixed to 40 //or use (150,300) to keep width fixed to 150 and varying height .
NSString *myString =#"Text for lable";
UIFont *myFont = [UIFont fontWithName:#"marker felt" size:14];// font used for label
CGSize myStringSize = [myString sizeWithFont:myFont
constrainedToSize:maximumSize
lineBreakMode:label.lineBreakMode];
use MySTringSize to set the frame for label. And use nemberOfLines property for label, it might be helpfull.

Calculating multiline text height for UILabel/UITableViewCell: different results when calculating vs actual drawing

This general topic has been asked here multiple times: how to render UITableViewCells with varying amount of text and thus varying height. The canonical answer is: you calculate the height in table view controller delegate in heightForRowAtIndexPath using sizeWithFont:constrainedToSize:lineBreakMode:. Later, the cell gets drawn, and you use something like [label sizeToFit] if needed, and all works like magic.
My problem: I am getting wrapping for some cells because sizeWithFont: returns different dimensions from actual drawing.
A specific example:
The text is this: "People forget that #BillGates had a sexy 1/4-inch thick slate back in 1993 from NEC. Whatever happens this week will NOT be about hardware!"
CGSize theSize = [text sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:CGSizeMake(310.0f, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
NSLog(#"calculated size for %#: %f, %f",text, theSize.width, theSize.height);
This returns: 306.000000, 84.000000. (I.e 4 rows with 17px font and 4px linespacing, 21px leading.) Good.
However, later when actually drawing the cell:
label = (UILabel *)[cell viewWithTag:3];
label.text = [NSString stringWithFormat:#"%#", text];
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont systemFontOfSize:17.0f];
CGSize labelSize;
labelSize = label.frame.size;
NSLog(#"label size before resizing: %f, %f", labelSize.width, labelSize.height);
[label sizeToFit];
labelSize = label.frame.size;
NSLog(#"label size after resizing: %f, %f for text %#", labelSize.width, labelSize.height,text);
(UILabel is loaded as part of UITableViewCell from NIB. In IB I set it to 310px wide.)
This should return exactly the same size as above. Instead, I get 281.000000, 105.000000 as the dimensions after sizeToFit call. It is now 5 lines at drawing time instead of 4, and the text spills over, I see the spillover in the UI.
So, for the same text, I am getting two different dimensions calculated, and can't figure it out. Is it something about UILabel? Does it have some inner margins? This keeps happening for some texts but not others, and I have not traced it to something particular about the strings; seems random. This topic highlights that there are two processing passes: calculating height vs actual drawing. This is consistent with what I'm seeing. But I don't understand what exactly is going on or how to fix it.
The question: why am I seeing two different calculated sizes, and how do I fix it?
Of course, the solution is obvious 30 seconds after posting. Maybe useful to others too...
The size by sizeWithFont: was correct. The sizes I calculated in the above way were incorrect, because [label sizeToFit] reduces the width of the label's frame. At subsequent calls to the same code, it started off with the frame that may already have been reduced.
The fix was to simply reset the frame width to a known good width before sizing to fit:
CGRect labelFrame = label.frame;
labelFrame.size.width = 310;
label.frame = labelFrame;
[label sizeToFit];
For multiline labels you need set
cell.textLabel.numberOfLines = 0;
and then
[cell.textLabel sizeToFit];
But for pretty view you need add some padding pixels. And your app will be awesome!
titleSize = [title sizeWithFont:[UIFont systemFontOfSize:(CGFloat)17.0]
constrainedToSize:CGSizeMake(280, 2000)
lineBreakMode:NSLineBreakByWordWrapping];