将 space 添加到字符串后 Space 未显示在标签上

Space is not showing on label After adding space to a string

Space 在向字符串添加 space 后未显示在标签上。我正在使用以下方法来获取它。我想在字符串后添加 space,它应该显示在标签上。

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

但是当我 运行 它只显示为 "ABCD"。

我在 tableviewcell 上显示标签,我将文本对齐设置为右

应该是

text=[text stringByAppendingString:@" "];

更新

任何追加方法都一样,我猜你需要一些space试试这个

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

谢谢。

试试这个:

text = [NSString stringWithFormat:@"%@ ", text];
NSString *yourString = @"ABCD";    
NSString *spaceString = @" ";
NSString *finalString = [NSString stringWithFormat:@"%@%@",yourString,spaceString];

签入设计。我已经检查了你的代码和 NSLog。用于测试

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

NSLog(@"%@", text);

添加了子类 UILabel 并添加了 edgeInsets 属性。

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];    

请试试这个

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;

如果您想更改标签的文本颜色而不是更改:

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