How to find the end coordinates of UILabel? - iphone

So I am dynamically adding UILabels to a view along a "timeline". What I would like to do is figure out how to put the starting coordinates of the next UILabel a couple of pixels past where the last one was. I'm having a hard time figuring out where the last one ended. I've looked at a few similar questions and I can't seem to grasp how to do it. Everything is taking place in a for loop so I can update the xcoordinate variable each loop, but I need to know how to get the label size. Anyone have any ideas? I tried this but it didn't seem to work:
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(labelsXpoint,
topLabelYpoint,
labelWidth,
20)];
CGSize labelSize = [title.text sizeWithFont:title.font constrainedToSize:title.frame.size
lineBreakMode:title.lineBreakMode];
labelsXpoint += labelSize.width;

This is how you would get the top right & bottom right coordinates of the title label:
CGPoint topRight = CGPointMake(title.frame.origin.x + title.frame.size.width, title.frame.origin.y);
CGPoint bottomRight = CGPointMake(title.frame.origin.x + title.frame.size.width, title.frame.origin.y + title.frame.size.height);

I would set the frame after you alter labelsXpoint. Something like:
UILabel *title = [[UILabel alloc]init];
CGSize labelSize = [title.text sizeWithFont:title.font constrainedToSize:title.frame.size lineBreakMode:title.lineBreakMode];
labelsXpoint += labelSize.width;
title.frame = CGRectMake(labelsXpoint, topLabelYpoint, labelWidth, 20);

You can use the functions: CGRectGetMaxX and CGRectGetMaxY:
CGPoint topRight = CGPointMake(CGRectGetMaxX(title.frame), title.frame.origin.y);
CGPoint bottomRight = CGPointMake(CGRectGetMaxX(title.frame), CGRectGetMaxY(title.frame));

Related

How to create square SKLabelNode in Sprite kit

How am I able to create an SKLabelNode and set the width and height of it. I have thought about just making a sprite image with the word on and then position that sprite but I don't believe that this is the best way of doing this.
Image:
http://i.imgur.com/dAP6yeT.png
Thanks!
I think that the best way of doing what you are asking is doing what you said. Just make an image of the word. This makes it to where you can also use it as a button. It might subtract some fps, but only if you have about 50+ nodes in your scene. Which is not likely since most people put a label on the menus. But if you do want to resize the SKLabelNode just resize the tex like this. label.fontsize = 20
The following code will add a node with the text you supply with a box around it. The border will be a rectangle that corresponds to the size of the text. If you want it square, just adjust the borderPath points to only use the labelNode.frame.size.width/2 for both horizontal and vertical spacing. Adjust the +/- 10 to the amount of spacing around the text you want.
//create the wrapper node
float initialWidth = 50;
float initialHeight = 50;
SKSpriteNode *parentNode = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(initialWidth, initialHeight)];
//create the label node
NSString *FontName = [UIFont boldSystemFontOfSize:10].fontName;
SKLabelNode *labelNode = [SKLabelNode labelNodeWithFontNamed:FontName];
labelNode.text = #"some text";
labelNode.fontColor = [UIColor blackColor];
labelNode.position = CGPointMake(0, -labelNode.frame.size.height/4);
//create the border node
SKShapeNode *borderNode = [SKShapeNode new];
UIBezierPath* borderPath = [[UIBezierPath alloc] init];
[borderPath moveToPoint:CGPointMake(-labelNode.frame.size.width/2 -10, -labelNode.frame.size.height/2 -10)];
[borderPath addLineToPoint:CGPointMake(labelNode.frame.size.width/2 +10, -labelNode.frame.size.height/2 -10)];
[borderPath addLineToPoint:CGPointMake(labelNode.frame.size.width/2 +10, labelNode.frame.size.height/2 +10)];
[borderPath addLineToPoint:CGPointMake(-labelNode.frame.size.width/2 -10, labelNode.frame.size.height/2 +10)];
[borderPath addLineToPoint:CGPointMake(-labelNode.frame.size.width/2 -10, -labelNode.frame.size.height/2 -10)];
borderNode.path = borderPath.CGPath;
borderNode.lineWidth = 10.0;
[borderNode setStrokeColor:[UIColor blackColor]];
//add it all together and present it
[parentNode addChild:borderNode];
[parentNode addChild:labelNode];
parentNode.position = newShapeScenePoint;
parentNode.size = CGSizeMake(labelNode.frame.size.width +10, labelNode.frame.size.height +10);
[myScene addChild:parentNode];
You can move the entire thing with
parentNode.position = CGPointMake(someNewX, someNewY)
You can resize the entire thing (including the text) with
float bigger = 3.0; //or whatever size you want
parentNode.xScale = bigger;
parentNode.yScale = bigger;

iOS: CGRectMake doesn't change position of a UILabel

I've created a label via storyboard. The order is like this:
View -> Scroll View -> Bunch of labels.
Here is the text output I want to achieve:
2 December <30 days left>
the <> enclosed text is in smaller letters an in a different UI Label.
Since the month name is dynamic and the width changes I've tried using CGRectMake to alter the position of the <> enclosed text.
But it isn't working. I tried changing the parameters of the function but the label stays put. It doesn't move from its initial position set in storyboard.
label_days_left.frame = CGRectMake(50, 10, 100, 99);
EDIT:
- (void) alignDaysLeft{
//code to align the label just after the match date
CGFloat dateLabelWidth = [label_date.text sizeWithFont:label_date.font].width;
CGFloat dateLabelHeight = [label_date.text sizeWithFont:label_date.font].height;
CGFloat someGap = 0;
CGFloat daysLeftX = label_date.frame.origin.x + dateLabelWidth + someGap;
CGFloat daysLeftY = label_date.frame.origin.y;
[label_date sizeToFit];
//label_days_left.frame = CGRectMake(daysLeftX, daysLeftY, 100, dateLabelHeight);
label_days_left.frame = CGRectMake(50, 10, 100, 99);
label_days_left.text = [NSString stringWithFormat:#"lolo"];
NSLog(#"date width - %f", daysLeftX);
}
label_date is the variable which contains the month and day.
label_days_left is the label variable which I am trying to place next to the month.
EDIT 2:
EDIT 3:
I created a new project.
Dragged a label on the view.
Created an outlet.
Synthesized it.
And used the following code to move it -
[labia setFrame:CGRectMake(0, 100, 5, 99)];
It didn't work. What mistake am I doing?
Do you have AutoLayout turned on? If so then that might be your problem, as you are not supposed to worry about setting the frame with AutoLayout, and instead set constraints.
NSString *someString = [NSString stringWithFormat:#"%#",label_date.text]
UIFont *yourFont = [UIFont systemFontOfSize:22.0];
CGSize stringBoundingBox = [someString sizeWithFont:yourFont];
[label_days_left setFrame:CGRectMake(label_days_left.frame.origin.x, 10, stringBoundingBox.width, 99)];
[label_days_left setText:someString];
Pls try this once....

How can i get rotate UIImageView with changable angle?

I have created one UIImageView
Here's code
capView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kARROW_CAP_NAME]];
capView.frame = CGRectMake(0, 0, kARROW_H, kARROW_H);
[self addSubview:capView];
Now I have two points which I need to move around View, So, they are Updating..
PointA, PointB
I got the angle between them:
CGFloat angleBetweenPoints(CGPoint first, CGPoint second)
{
CGFloat height = second.y - first.y;
CGFloat width = first.x - second.x;
CGFloat rads = atan(height/width);
return RADIANS_TO_DEGREES(rads);
}
When I apply this angle to my UIImageView, it continuously change,
My used code here:
capView.transform = CGAffineTransformMakeRotation(arrowAngle);
or
capView.transform = CGAffineTransformRotate(self.transform, arrowAngle);
Arrow Angle is the value of above function which i write above..
Please take a look and help me.
Got the answer, So simple..
But still no one give me answer..
capView.transform = CGAffineTransformMakeRotation(RADIANS(M_PI_2 - arrowAngle))
And RADINAS is macro define here,
#define RADIANS(degrees) ((degrees * M_PI) / 180.0)
Thanks to all who be a part of it..

CGRect Grid on screen?

I want to create a grid of rectangles at the centre of screen leaving some space on the edges. The need of that arises because I am spewing different sprites at random points and they keep spawning on top of eat other. So i thought if there is a way of creating a class that creates the grid and returns me with a random rect and mark it occupied as long at the sprite stays in that rect and make it free after.
If i can get some help or any tips it will be great. Any other solutions to achieve this are welcome too.
Thanks.
You could nest two for loops, one for rows and one for columns, make them both run 5 times, and in each loop increment the x position and y position by one-fifth the width and height of the screen and put these coordinates into a CGRrect. That would do what you want.
Thanks #andrewx for your help. This will create CGRect in the given range and then return a random one.
-(void) makeCGRectArray{
rectsArray = [[NSMutableArray alloc] init];
for (int x = 30; x<=420; x= x+60) {
for (int y=40; y<=280; y=y+40) {
CGRect newRect = CGRectMake(x, y, 60, 40);
[rectsArray addObject:[NSValue valueWithCGRect:newRect]];
}
}
[self getRandomCgrect:rectsArray];
}
-(CGRect) getRandomCgrect:(NSMutableArray*) rectArray{
NSInteger randomPoint = (arc4random() % (49));
CGRect randomRect = [[rectsArray objectAtIndex:randomPoint] CGRectValue];
self.isOccupied = YES;
return randomRect;
}

How to resize scrollView content and put textViews in that according to their text size? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can i fix my scrollView dynamically when i have more than one textViews in it?
I made a scrollView in interface builder and put 3 textViews and 2 button in that. Sometimes textViews have more text and sometimes less. i managed to give them flexible frame size. but if top textView has more text then some of its text comes over 2nd textView. i tried this code. i think it should work but its not working properly.
storyTitle.text = sTitle;
storyTitle.font = [UIFont boldSystemFontOfSize:18];
date.text = sDate;
date.font = [UIFont systemFontOfSize:16];
summary.text = sSummary;
summary.font = [UIFont systemFontOfSize:16];
CGRect titleFrame = storyTitle.frame;
titleFrame.size.height = storyTitle.contentSize.height;
storyTitle.frame = titleFrame;
CGRect dateFrame = date.frame;
dateFrame.size.height = label.contentSize.height;
date.frame = dateFrame;
CGRect summaryFrame = summary.frame;
summaryFrame.size.height = summary.contentSize.height;
summary.frame = summaryFrame;
CGRect topTextViewFrame = storyTitle.frame;
CGRect middleTextViewFrame = date.frame;
NSLog(#"size: %d",topTextViewFrame.origin.y);
middleTextViewFrame.origin.y = topTextViewFrame.origin.y + topTextViewFrame.size.height;
CGRect bottomTextViewFrame = summary.frame;
bottomTextViewFrame.origin.y = middleTextViewFrame.origin.y + middleTextViewFrame.size.height;
// then adjust other controls based on these frames, for example:
CGRect myButtonFrame = detailButton.frame;
myButtonFrame.origin.y = bottomTextViewFrame.origin.y + bottomTextViewFrame.size.height;
CGRect myButtonFrame2 = fbButton.frame;
myButtonFrame2.origin.y = myButtonFrame.origin.y + myButtonFrame.size.height;
// finally adjust the contentSize of the scrollview assuming the button is the bottom element
CGSize csize = scrollView.contentSize;
csize.height = myButtonFrame2.origin.y + myButtonFrame2.size.height;
scrollView.contentSize = csize;
can anyone tell me whats wrong with this? and how should i right it so it ll work fine. thanx in advance
hi if your text is not editable by user the try using UILabel instead of UITextView.If you set numberOfLines property of UILabel = 0 then it can go to more that one line and then u can calculate the required height for the label by the method
CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(label.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
the u can set the frame of label according to height you get form here like
label.frame = CGRectMake(label.frame.origin.x,label.frame.origin.y,label.frame.size.width,size.height);
and if you have such multiple labels the uyou can do following
CGSize size = [label1.text sizeWithFont:label1.font constrainedToSize:CGSizeMake(label1.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label1.frame = CGRectMake(label1.frame.origin.x,label1.frame.origin.y,label1.frame.size.width,size.height);
size = [label2.text sizeWithFont:label2.font constrainedToSize:CGSizeMake(label2.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label2.frame = CGRectMake(label2.frame.origin.x,label1.frame.origin.y+label1.frame.size.height+10,label2.frame.size.width,size.height);
size = [label3.text sizeWithFont:label3.font constrainedToSize:CGSizeMake(label3.frame.size.width, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap ];
label3.frame = CGRectMake(label3.frame.origin.x,label2.frame.origin.y+label2.frame.size.height+10,label3.frame.size.width,size.height);
and finaly you can adjust the content height of your scroll view according tho last label.