首页 文章

限制VoiceOver从UILabel读取的文本

提问于
浏览
4

我正在创建一个类似于Mail(或Messages或Notes)的应用程序,它显示包含消息预览的 UITableViewCell . 文本通常不适合 UILabel ,因此文本被截断并自动显示省略号 . 这在我的应用中适用于有视力的用户,但是当使用VoiceOver时, UILabel 的整个 UILabel 内容会被大声朗读 . 在Mail中不会发生这种情况 - VoiceOver会在到达省略号时停止宣布文本 .

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

cell.messagePreviewLabel.text = a_potentially_really_long_string_here

1 回答

  • 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
    

相关问题