限制 VoiceOver 从 UILabel 读取的文本

Jor*_*n H 5 uitableview uilabel ios voiceover

我正在创建一个类似于“邮件”(或“消息”或“便笺”)的应用程序,它显示UITableViewCell包含消息预览的 s。文本通常无法全部放入 中UILabel,因此文本会被截断并自动显示省略号。这在我的应用程序中对于视力正常的用户来说效果很好,但是当使用 VoiceOver 时,会大声朗读整个text内容。UILabel这在邮件中不会发生 - VoiceOver 在到达省略号时停止播报文本。

如何在我的应用程序中获得与 Mail 相同的行为 - 强制 VoiceOver 在到达省略号时停止播报文本?

cell.messagePreviewLabel.text = a_potentially_really_long_string_here

Chr*_*sCM 0

这是 UILabel 的子类,它将执行您想要的操作。请注意,我已经零努力地对此进行了优化。这部分取决于你。从可访问性的角度来看,我的总体建议仍然是简单地保留它。从 A11y 的角度来看,覆盖辅助功能标签以仅描绘实际标签中文本的一部分是一件非常值得怀疑的事情。使用这个工具要非常小心!

@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
Run Code Online (Sandbox Code Playgroud)