Aligning label relative to another dynamically created label - iphone

I am writing a code that dynamically creates labels and realigns the labels relative to the last label created. I am using the following code to create and resize the height of the label to fit the content.
// Create label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = SomeVariatingTextContent;
[self.scrollView addSubview:label];
// Resize Label
UIFont* font = label.font;
CGSize constraintSize = CGSizeMake(label.frame.size.width, MAXFLOAT);
CGSize labelSize = [label.text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeTailTruncation];
label.frame = CGRectMake(label.frame.origin.x, label.frame.origin.y, 280, labelSize.height);
How do find the identifier to the previous label such that I can realign the the next label being created such that it is always 8 points below the previous label created?

Try to use CGRectGetMinY etc. (look here: CGGeometry Reference to get the edges of previous UILabel, for example:
CGFloat *yPos = CGRectGetMinY(previousLabelRect);
nextLabel.frame = CGRectMake(x,yPos+8,width,height);
EDIT
I can assume that the last UILabel is the last one you added to the view so you can:
UILabel lastLabel = [[self.view subviews]lastObject];
Br aware that if you have other subviews added later you need to filter the array to get the UILabels only. if the order is the problem you can get all the labels and check their y position to get the last one.
An other way is too store save a pointer to the UILabel at the end of the loop that creates the labels, and update that pointer every time a new label is created.

Related

how to make UILabel dynamically without overlapping with other UILabel

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!

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

Multiline Text On UIsegment Control

I have a UISegmentControl with default style (White). I want to add text on it. But the text that i want to put on it is a long text.
I have to show the text in 2 lines of a segment. But i dont have to raise the width of the segment Because of screen width limit & no of segments.
I had tried to put a label on segment control programmatically, but my label is not displayed. Although we can put a label on segment control using XIB. but due to dynamic nature of text & segment control, I have to draw the segment control programmatically & also put the text on it.
Guidance will be appreciated.
Hi friend segment controller already have the label as the subview, so this code is helpful to achieve the multiline text to segment control
for (id segment in [segmentedControl subviews])
{
for (id label in [segment subviews])
{
if ([label isKindOfClass:[UILabel class]])
{
//hear u add any of delegate function to increase the height and other label functionality in this
[label setTextAlignment:UITextAlignmentCenter];
[label setFont:[UIFont boldSystemFontOfSize:12]];
//to adjust the label size manually with respect to text use below code
CGSize labelSize = CGSizeMake(100, 80);
CGSize theStringSize = [label.text sizeWithFont:label.font constrainedToSize:labelSize];
CGRect frame = label.frame;
frame.size = theStringSize;
}
}
}
Have a Good day

iphone : How to draw line on Label?

I want to make my label as shown in the image
I know I can get this effect by putting image view on it.
but is there any other method to do ?
How can I put line on label ?
Try this,
UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 271, 26)];
blabel.text = #"Hellooooooo";
blabel.textAlignment = UITextAlignmentCenter;
blabel.backgroundColor = [UIColor clearColor];
blabel.textColor = [UIColor blackColor];
blabel.font = [UIFont systemFontOfSize:14];
[scrollDemo addSubview:blabel];
//underline code
CGSize expectedLabelSize = [#"Hellooooooo" sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((blabel.frame.size.width - expectedLabelSize.width)/2, expectedLabelSize.height + (blabel.frame.size.height - expectedLabelSize.height)/2, expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor blackColor];
[scrollDemo addSubview:viewUnderline];
[viewUnderline release];
The line above will appear below the text. You just need to change Y for UIView and it'll do wonders :)
put another label with "_" over it
transparent background.
you can create UIView with line's height and width and give background color to it. Put UIView over your UILabel .
For one of my projects I've created an UILabel subclass, which supports multiline text, underline, strikeout, underline/strikeout line offset, different text alignment and different font sizes.
Please see provided link for more info and usage example.
https://github.com/GuntisTreulands/UnderLineLabel
Place a UIImageView with line image on your label so when you run application it will fit.

How to display an array of UILabels?

I have a list of items I'd like to show in a UITableViewCell. Right now, I'm simply using a cell.textLabel with commas separating each value, but I'd like to do something a little more dynamic.
How would I achieve something like this?
Would it be an array of UILabels with borders and radius on those borders?
Thanks for your ideas.
Here's a possible quick and easy way to do this. It's based on the code that you can get here.
Note that you have to add the QuartzCore framework to your project and include in the file where you write this code!
Every UIView is backed by a CALayer. You can get at the UIView's CALayer with the .layer property. Since a UILabel is a UIView, you can get its backing layer this way. Once you have the backing layer, you can set its backgroundColor, cornerRadius, borderColor, and borderWidth properties. That should let you create the rounded effect.
To get the centered effect, try setting the UILabel's textAlignment to UITextAlignmentCenter. Then, you could try setting the UILabel's frame based on sizeThatFits, or maybe based on calling sizeWithFont on the string you're putting into the label.
Here's some quick, totally untested code to get you started.
Assume that somewhere you've initialized a UIFont as follows (put in whatever size you want for the font).
labelFont = [UIFont systemFontOfSize:14];
Then, for each label, set it up as follows.I'm assuming you've pulled the text out of an array and put into a variable called "text". X_PADDING and Y_PADDING are how much spacing you want around the label's text. xLoc and yLoc are variables you're using to keep track of the x and y position you want to put the labels at. You'll probably increase xLoc based on textSize + X_PADDING + LABEL_SPACING or something (where you define LABEL_SPACING):
CGSize textSize = [text sizeWithFont:labelFont];
CGRect frame = CGRectMake( xLoc, yLoc,
textSize.width + X_PADDING,
textSize.height + Y_PADDING);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = text;
label.textAlignment = UITextAlignmentCenter;
CALayer *layer = label.layer;
layer.masksToBounds = YES;
layer.cornerRadius = 7.0; // or whatever works for you
layer.borderWidth = 1.0;
layer.borderColor = [[UIColor redColor].CGColor;
layer.backgroundColor = [UIColor blueColor].CGColor;
// Add the layer into its superview
[yourSuperview addSubview:label];
I hope this helps get you started.