Search code examples
iosuilabelnstimeintervaluiaccessibility

Proper way to calculate time for an accessibility label that updates when a UISlider value is changed?


So I am using UISlider for an Audio Player's scrubber. What I am attempting to do is update a label whenever a UISlider's value is changed so that it can properly display the current audio's time elapsed in minutes and seconds. Here is my current code:

- (IBAction)valueChanged:(id)sender {
    if (!self.scrubbing) {
        [[CRAudioUtility sharedUtility]seekToTime:self.progressSlider.value];
        NSInteger value    = [CRAudioUtility sharedManager].currentPlaybackTime;
        NSInteger minutes    = value / 60;
        NSInteger seconds = value % 60;

        NSString *time_stamp = [NSString stringWithFormat:@"%d hours:%d minutes",minutes,seconds];
        [self.progressSlider setAccessibilityValue:time_stamp];
        self.progressSliderLabel.text = [NSString stringWithFormat:@"%d:%d minutes",minutes,seconds;

    }
}

Audio items may or may not be longer than an hour, and the length may be more than one hour.

Since the accessibilityLabel is announced, I need to ensure that the word "hour" is announced properly in their singular or plural format. What kind of solutions can I do to handle this case?


Solution

  • What kind of solutions can I do to handle this case?

    Seems like you'd just want to consider the various possibilities separately:

    if (minutes > 120) {
        time_stamp = [NSString stringWithFormat:@"%d hours:%d minutes", minutes / 60, minutes % 60];
    }
    else if (minutes > 60) {
        time_stamp = [NSString stringWithFormat:@"1 hour:%d minutes", minutes % 60];
    }
    else {
        time_stamp = [NSString stringWithFormat:@"%d:%d minutes", minutes, seconds];
    }