-1

Space is not showing on label After adding a space to a string. I am using the following method to get it. I want to add space after string and that should be displayed on label.

NSString *text = @"ABCD";
text = [text stringByAppendingString:@" "];

But when i run it is showing as "ABCD" only.

I am displaying label on tableviewcell and i am setting text alignment to right

VJVJ
  • 435
  • 3
  • 21

6 Answers6

0

It should be

text=[text stringByAppendingString:@" "];

Update

Any append method would work same, I guess you need some space try this

text=[text stringByAppendingString:@"\t"];

Thanks.

iphonic
  • 12,615
  • 7
  • 60
  • 107
0

Check in design. I have checked your code and NSLog it. For testing

NSString *text = @"ABCD";
text = [text stringByAppendingString:@" test"];

NSLog(@"%@", text);
Shivani Gor
  • 329
  • 1
  • 8
0

Added subclass UILabel and added an edgeInsets property.

customLabel.h

#import <UIKit/UIKit.h>

@interface customLabel : UILabel

@property (nonatomic, assign) UIEdgeInsets edgeInsets;

@end

customLabel.m

#import "customLabel.h"

@implementation customLabel

- (id)initWithFrame:(CGRect)frame{
 self = [super initWithFrame:frame];
 if (self) {
    self.edgeInsets = UIEdgeInsetsMake(0, 0, 0, 2);
 }
 return self;
}

- (void)drawTextInRect:(CGRect)rect {
  [super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}

@end

ViewController.m

title = [[customLabel alloc] initWithFrame:CGRectMake(75, 25, 200, 14)];
[title setTextColor:[UIColor whiteColor]];
[title setFont:[UIFont systemFontOfSize:14]];
[title setText:@"Text"];
[self.view addSubview:title];    
VJVJ
  • 435
  • 3
  • 21
  • I got solution for my question from this link http://stackoverflow.com/questions/9502235/how-to-add-padding-left-on-a-uilabel-created-programmatically – VJVJ Sep 23 '15 at 10:11
0

Please Try this

NSString *text = @"ABCD";
text = [text stringByAppendingString:@" "];

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@.", text]];

[string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,string.length-1)];

[string addAttribute:NSForegroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(string.length-1,1)];
label.attributedText = string;

If you want to change text color of label than change in:

[string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,string.length-1)];
Mehul Sojitra
  • 1,181
  • 9
  • 15
-1

Try this:

text = [NSString stringWithFormat:@"%@ ", text];
Anand V
  • 423
  • 3
  • 13
-1
NSString *yourString = @"ABCD";    
NSString *spaceString = @" ";
NSString *finalString = [NSString stringWithFormat:@"%@%@",yourString,spaceString];
Gaby Fitcal
  • 1,814
  • 1
  • 17
  • 28