为什么在使用replacementString时UILabel中的文本似乎有空格?

时间:2013-07-20 17:13:42

标签: ios nsstring uilabel

我用它来填写UILabel:

storeName.text = [self.receivedLocation.name stringByReplacingOccurrencesOfString:@"Store" withString:@""];

但这导致在标签的左侧插入一个明显的空间。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果您的字符串的值为Store some more stuff,则结果将为some more stuff,前导空格仍然存在。

试试这个:

// Get original text
NSString *label = self.receivedLocation.name;
// Remove occurrences of "Store"
label = [label stringByReplacingOccurrencesOfString:@"Store" withString:@""];
// Trim any leading and/or trailing whitespace
label = [label stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
storeName.text = label;