gpt4 book ai didi

ios - 如何阻止 UILabel 最后修剪空间?

转载 作者:可可西里 更新时间:2023-11-01 05:33:58 24 4
gpt4 key购买 nike

我有一个 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 的文本是阿拉伯语。

最佳答案

有点 hack,但一个解决方案是在标签中使用属性字符串并在字符串末尾包含一个透明句点 (".")。

只需替换您的 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;
}

关于ios - 如何阻止 UILabel 最后修剪空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27556333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com