如何使UILabel不剪切末尾的空格?

4

我有一个像走马灯一样的UILabel,每0.09秒文本就会改变,但当最后一个字符是空格时,它会被裁剪掉,导致走马灯效果看起来很卡顿。

以下是代码:

[self setTickerLabel: [ [UILabel alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 40)]];


[self.tickerLabel setFont:[UIFont fontWithName:@"Courier" size:TICKER_FONT_SIZE]];

self.text =[NSString stringWithFormat:@"%@",self.result];


self.tickerLabel.textAlignment = NSTextAlignmentRight;

[self.tickerLabel setText:self.text];

[self.tickerLabel setLineBreakMode:NSLineBreakByWordWrapping];


[NSTimer scheduledTimerWithTimeInterval:TICKER_RATE target:self selector: @selector(nudgeTicker:) userInfo:nil repeats:YES];

[self.view addSubview:self.tickerLabel];

nudge Ticker方法的作用如下:
NSString* firstLetter = [self.text substringWithRange: NSMakeRange(0,1)];
NSString* remainder = [self.text substringWithRange:NSMakeRange(1,[self.text length]-1)];
self.text=[remainder stringByAppendingString: firstLetter];
self.tickerLabel.text=self.text;

我真的需要帮助。我该怎么解决这个问题?顺便说一下,UILabel 的文本是阿拉伯语。


0.09毫秒,真的吗?我想你是指90毫秒。 - rdelmar
什么是“trimmed”?标签是否有一些背景颜色,以便清楚地显示空间正在被删除?大小是否被裁剪或其他操作? - Mike Welsh
@rdelmar 是的,我指的是0.09秒。 - Eddy
1个回答

7

有点取巧,但一种解决方法是在标签中使用带属性的字符串,并在字符串末尾包含一个透明的句号(“.”)。

只需替换您的nudgeTicker:方法即可。

- (void)nudgeTicker:(id)target {
    NSString* firstLetter = [self.text substringWithRange: NSMakeRange(0,1)];
    NSString* remainder = [self.text substringWithRange:NSMakeRange(1,[self.text length]-1)];
    self.text=[remainder stringByAppendingString: firstLetter];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@.", self.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)];
    self.tickerLabel.attributedText = string;
}

你是个天才。 - DawnSong
你是个天才。 - mayqiyue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接