Ultimate text read by VoiceOver from UILabel

I am creating an application similar to Mail (or messages or notes) that displays UITableViewCellthat contain a preview of the message. Usually the text is not suitable for UILabel, so the text is truncated, and the ellipsis is displayed automatically. This works well in my app for sighted users, however when using VoiceOver all text textin UILabelis read aloud. This does not happen in Mail - VoiceOver stops the text announcement when it reaches the ellipsis.

How can I get the same behavior in my application as Mail - forcibly disable VoiceOver by declaring text when it reaches ellipsis?

cell.messagePreviewLabel.text = a_potentially_really_long_string_here

+4
source share
1 answer

Here is a subclass of UILabel that will do what you want. Notice, I went to zero to optimize this. This part is up to you. My general recommendation, in terms of accessibility, would still be to just leave it. Overriding the accessibility label to display only part of the text in the actual label is a very dubious task from the point of view of A11y. Use this tool very carefully!

@interface CMPreivewLabel : UILabel
@end

@implementation CMPreviewLabel

- (NSString*)accessibilityLabel {
    return [self stringThatFits];
}

- (NSString*)stringThatFits {
    if (self.numberOfLines != 1) return @"This class would need modifications to support more than one line previews";

    const int width = self.bounds.size.width;

    NSMutableAttributedString* resultString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];

    while (width < resultString.size.width) {
        NSRange range = [resultString.string rangeOfString:@" " options:NSBackwardsSearch];

        range.length = resultString.length - range.location;

        [resultString.mutableString replaceCharactersInRange:range withString:@""];
    }

    return resultString.string;
}

@end
0
source

Source: https://habr.com/ru/post/1584702/


All Articles