Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add spaces after a string

I am wanting to add spaces after a string in objective c....so I can perform some padding.

I have viewed the method at this question - How can I add leading whitespace in front of the NSString?

But it only seems to work for spaces in front of a NSString.

Edit: These don't seem to be working, here is some information.

I am performing this inside - (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath - Where I have an image being displayed also to the right of the text. But my text is too close to the image. I know I can create a UITextView to deal with this, but this then leads to other issues. Hence the spaces at the end of the text being the simplest solution....but this does not seem to work inside a cell.

like image 693
Remixed123 Avatar asked Aug 23 '26 18:08

Remixed123


1 Answers

You can adapt the answer you found very quickly to get the answer you want.

#import <Foundation/Foundation.h>
#include <stdlib.h>

int main()
{
    int maxLength = 5;
    NSString *text = @"xml";
    NSLog(@"Text:\"%@\"", text);

    NSString *result = [text stringByPaddingToLength:MAX(maxLength, text.length)
                                          withString:@" " startingAtIndex:0];
    NSLog(@"Result:\"%@\"", result);
    return 0;
}

I've created an SSCCE and added logs to show that this method works.

2014-09-07 16:25:06.713 a.out[29685:507] Text:"xml"
2014-09-07 16:25:06.714 a.out[29685:507] Result:"xml  "
like image 120
James Webster Avatar answered Aug 25 '26 08:08

James Webster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!