Question

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.

Was it helpful?

Solution

Try with following code

NSString *contentOffSet = [NSString stringWithFormat:@"%@", NSStringFromCGPoint([self.mainScrollView contentOffset])];;
NSString *removeBreckets = [contentOffSet stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@"{}"]];
label.text = removeBreckets;

OTHER TIPS

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top