Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop UILabel from trimming the space at the end?

I have a UILabel acting like a ticker so every 0.09 seconds the text is being changed, but when a space comes at the end of the label it is being trimmed, so it is looking like the ticker is lagging.

Here’s the code:

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

The nudge Ticker method does the following:

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;

I really need some help. How can I fix this? By the way the text of the UILabel is in Arabic.

like image 407
Eddy Avatar asked Mar 18 '23 14:03

Eddy


1 Answers

A bit of a hack, but one solution would be to use an attributed string in your label and include a transparent period (".") at the end of the string.

Just replace your nudgeTicker: method.

- (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;
}
like image 192
picciano Avatar answered Apr 01 '23 00:04

picciano