0

I have 4 different buttons for every corner of the screen. Just need to show their coordinates for every device changes in a text field or a label.

“x0” - button x coordinate on the screen “y0” - button y coordinate on the screen

i should show like x = "x0" ; y = "y0"

dicle
  • 1,122
  • 1
  • 12
  • 40

2 Answers2

0

There is an easy built-in way to log frames. Use NSStringFromCGRect(frame); to show the coordinates of the button.

Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
  • Can i set this adding additional info like adding X: self.lblForInfo.text = @"X:"(NSStringFromCGRect(_firstButton.frame)); @FabioBerger – dicle Oct 26 '15 at 16:15
  • Is that a question? You can set the text with a formatted string like this: ```self.lblForInfo.text = [NSString stringWithFormat:@"X:%@",NSStringFromCGRect(_firstButton.frame)];``` – Fabio Berger Oct 26 '15 at 16:21
  • okay what about when i want to show more than one info in a same textfield ... ? self.lblForInfo.text = [NSString stringWithFormat:@"X2:%@", NSStringFromCGRect(_secondButton.frame), @"X1:%@",NSStringFromCGRect(_firstButton.frame)]; @FabioBerger – dicle Oct 26 '15 at 17:11
  • almost, it would be like this ```self.lblForInfo.text = [NSString stringWithFormat:@"X2:%@ X1:%@", NSStringFromCGRect(_secondButton.frame),NSStringFromCGRect(_firstButton.frame)];``` – Fabio Berger Oct 27 '15 at 06:55
0

Although you can directly print a CGRect using NSStringFromCGRect, here is a sample of what you are looking for. I'm just putting button1's coordinates in text field.

UIButton *button1, *button2, *button3, *button4;

CGFloat button1X = button1.frame.origin.x;
CGFloat button1Y = button1.frame.origin.y;

CGFloat button2X = button2.frame.origin.x;
CGFloat button2Y = button2.frame.origin.y;

CGFloat button3X = button3.frame.origin.x;
CGFloat button3Y = button3.frame.origin.y;

CGFloat button4X = button4.frame.origin.x;
CGFloat button4Y = button4.frame.origin.y;

NSString *button1Coords = [NSString stringWithFormat:@"x = \"%f\" x = \"%f\"", button1X, button1Y];

self.textField.text = button1Coords;
Abhinav
  • 37,684
  • 43
  • 191
  • 309