How to use AutoScrollLabel in UILabel inside inputAccessoryView?

1000 views Asked by At

How to use AutoScrollLabel in UILabel inside inputAccessoryView when the keyboard appears. I seem to have difficulties using it because I don't use an IB method to connect it to the code. I do this programatically. I have imported the .h file of AutoScrollView and it gives me errors. Find the AutoScrollLabel here

This is my code for the inputAccessoryView.

- (void)viewDidLoad
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake
(0, 0, 320, 23)];
label.backgroundColor = [UIColor clearColor];
label.shadowOffset = CGSizeMake(0, 1);
label.text = @"24 Hour time format only!";
label.font = [UIFont systemFontOfSize:17.0];
[label setTextColor:[UIColor whiteColor]];

UIBarButtonItem *text2 = [[UIBarButtonItem alloc] initWithCustomView:label];

UIToolbar* numberToolbar = [[UIToolbar alloc]init];
numberToolbar.barStyle = UIBarStyleBlackOpaque;
numberToolbar.items = [NSArray arrayWithObjects:text2, nil];
[numberToolbar sizeToFit];
2

There are 2 answers

26
Mani On BEST ANSWER

In my example, I've created label with xib, then I add it into keyboard acc when it's appear.

Declare label like

@property (strong, nonatomic) IBOutlet CBAutoScrollLabel *autoScrollLabel;

In viewDidLoad

self.autoScrollLabel.text = @"This text may be clipped, but now it will be scrolled. This text will be scrolled even after device rotation.";
    self.autoScrollLabel.textColor = [UIColor blueColor];
    self.autoScrollLabel.labelSpacing = 35; // distance between start and end labels
    self.autoScrollLabel.pauseInterval = 1.7; // seconds of pause before scrolling starts again
    self.autoScrollLabel.scrollSpeed = 30; // pixels per second
    self.autoScrollLabel.textAlignment = NSTextAlignmentCenter; // centers text when no auto-scrolling is applied
    self.autoScrollLabel.fadeLength = 12.f;
    self.autoScrollLabel.scrollDirection = CBAutoScrollDirectionLeft;
    [self.autoScrollLabel observeApplicationNotifications];

Then,add code to textfield delegate method textFieldDidBeginEditing: as below

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.inputAccessoryView = self.autoScrollLabel;
}

And finally, add code to textFieldDidBeginEditing: as below

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    textField.inputAccessoryView = nil;
}
0
user1045302 On

I to have had the text not scroll when I think it should. What I have discovered is that if the width of the label that displays the text is larger than the text to display it will not scroll. This bring up the issue where the displayed text is now truncated with "..." It scrolls, but it only shows the text up to and including the "..." If you change the font size to a smaller size, it works as expected, is this by design, I do not know. Other than those 2 issues, this is a very nice feature.