0

Im trying to convert the contentOffset from a ScrollView into a string.

I have tried many ways but the closest I got was:

label.text = [NSString stringWithFormat:@"%@", NSStringFromCGPoint([self.mainScrollView contentOffset])];

This worked but I the label has the value inside {}

{0, 0}

Does anyone know how to remove the {} or have a better way? Thanks

EDIT: about 30 seconds after asking I found this to work:

label.text = [NSString stringWithFormat:@"%02f KM", _mainScrollView.contentOffset.y];

Thanks for everyones help.

iPatel
  • 46,010
  • 16
  • 115
  • 137
Johann Burgess
  • 580
  • 6
  • 17

2 Answers2

1

Try with following code

NSString *contentOffSet = [NSString stringWithFormat:@"%@", NSStringFromCGPoint([self.mainScrollView contentOffset])];;
NSString *removeBreckets = [contentOffSet stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"{}"]];
label.text = removeBreckets;
iPatel
  • 46,010
  • 16
  • 115
  • 137
0

You can access the indiviual values of a CGPoint struct:

CGPoint point = CGPointMake(10, 100);
CGFloat x = point.x;
CGFloat y = point.y;

Use these in a string:

NSString *string = [NSString stringWithFormat:@"%lf, %lf", x, y];
Rich
  • 8,108
  • 5
  • 46
  • 59