我有以下格式的电话号码
1-1xx-2XX-9565
目前VO将其读作“一个(暂停)一个x x(暂停)两个x x(暂停)减去九千五百六十五”。
VO应将其读作“一个(暂停)一个x x(暂停)两个x x(暂停)九个五六五”。
可能是什么问题呢?这是错误的手机格式吗?
让我们分解正在发生的事情。 VoiceOver不知道您呈现的文本是电话号码,并将其视为文本句子。在该文本中,它试图找到不同的组件并适当地读取它们。例如,文本"buy 60 cantaloupes"有3个组件,“买”,“60”和“哈密瓜”。第一个是文本,读作文本,第二个是纯数字,最好读作“六十”,第三个读作文本。
(我不是在谈论实际的实施,只是推理。)
如果从左到右读取1-1xx-2xx-9565,则第一个不同的分量为“1”,其中自身为数字并读为“1”。如果电话号码以“12-1xx”开头,则第一个组件将被读作“十二”,因为它纯粹是数字。
下一个组件是“1xx”或“-1xx”,具体取决于您如何看待它。在任何一种情况下,它都是数字和字母的组合,例如它不是纯粹的数字,因此被读作文本。如果在该组件中包含“ - ”,则将其解释为未读出的连字符。这就是为什么永远不会读出该组件的“ - ”。下一个组件(“-2xx”)以相同的方式处理。
最后一个组件是“-9565”,结果证明是有效数字。正如在哈密瓜句子中看到的那样,VoiceOver将其读作数字,在这种情况下,“ - ”不再被解释为连字符,而是作为“减号”。
在应用程序中与Voice Over一起使用的任何标签,视图或其他元素上,当您了解有关如何阅读文本的更多信息时,可以提供自己的“辅助功能标签”。只需将您自己的字符串分配给accessibilityLabel
属性即可完成此操作。
现在,您可以通过多种不同方式创建合适的字符串,在您的情况下,非常简单的方法是在任何地方添加空格,以便单独读取每个数字。但是,它对我来说似乎有点脆弱,所以我继续使用数字格式器将各个数字翻译成文本表示。
NSString *phoneNumber = @"1-1xx-2xx-9565";
// we want to know if a character is a number or not
NSCharacterSet *numberCharacters = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
// we use this formatter to spell out individual numbers
NSNumberFormatter *spellOutSingleNumber = [NSNumberFormatter new];
spellOutSingleNumber.numberStyle = NSNumberFormatterSpellOutStyle;
NSMutableArray *spelledOutComonents = [NSMutableArray array];
// loop over the phone number add add the accessible variants to the array
[phoneNumber enumerateSubstringsInRange:NSMakeRange(0, phoneNumber.length)
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
// check if it's a number
if ([substring rangeOfCharacterFromSet:numberCharacters].location != NSNotFound) {
// is a number
NSNumber *number = @([substring integerValue]);
[spelledOutComonents addObject:[spellOutSingleNumber stringFromNumber:number]];
} else {
// is not a number
[spelledOutComonents addObject:substring];
}
}];
// finally separate the components with spaces (so that the string doesn't become "ninefivesixfive".
NSString *yourAccessiblePhoneNumber = [spelledOutComonents componentsJoinedByString:@" "];
我跑这个的结果是
one - one x x - two x x - nine five six five
如果您需要对您的电话号码进行其他修改以使其正确阅读,那么您可以这样做。我怀疑你将在你的应用程序中使用它不止一个位置,因此创建自定义NSFormatter可能是个好主意。
在iOS 7上,您还可以在属性字符串上使用UIAccessibilitySpeechAttributePunctuation
属性来更改它的发音方式。
Speech Attributes for Attributed Strings
可以应用于属性字符串中的文本以修改该文本发音方式的属性。
UIAccessibilitySpeechAttributePunctuation
此键的值是
NSNumber
对象,您应将其解释为布尔值。当值为YES
时,将使用文本中的所有标点符号。您可以将此用于标点符号相关的代码或其他文本。适用于iOS 7.0及更高版本。
在
UIAccessibilityConstants.h
宣布。
如果你想单独拼写所有字符,一个简单的解决方案是用逗号“,”分隔字符。
您可以使用String扩展来转换字符串:
extension String
{
/// Returns string suitable for accessibility (voice over). All characters will be spelled individually.
func stringForSpelling() -> String
{
return stringBySeparatingCharactersWithString(",")
}
/// Inserts a separator between all characters
func stringBySeparatingCharactersWithString(separator: String) -> String
{
var s = ""
// Separate all characters
let chars = self.characters.map({ String($0) })
// Append all characters one by one
for char in chars {
// If there is already a character, append separator before appending next character
if s.characters.count > 0 {
s += separator
}
// Append next character
s += char
}
return s
}
}
然后在代码中使用它,如下所示:
myLabel.accessibilityLabel = myString.stringForSpelling()
只需为最后一个数字的每个数字添加一个逗号,也可以在最后一个数字后面添加逗号。这将确保语音翻转读取与前一个数字相同的最后一个数字。
例如您的电话号码: - 1-1xx-2xx-9565可访问性标签: - 1-1xx-2xx-9,5,6,5,
这是Swift中的代码
public func retrieveAccessiblePhoneNumber(phoneNumber: String) -> String {
// We want to know if a character is a number or not
let characterSet = NSCharacterSet(charactersInString: "0123456789")
// We use this formatter to spell out individual numbers
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .SpellOutStyle
var spelledOutComponents = [String]()
let range = Range<String.Index>(start: phoneNumber.startIndex, end: phoneNumber.endIndex)
// Loop over the phone number add add the accessible variants to the array
phoneNumber.enumerateSubstringsInRange(range,
options: NSStringEnumerationOptions.ByComposedCharacterSequences) { (substring, substringRange, enclosingRange, stop) -> () in
// Check if it's a number
if let substr = substring where substr.rangeOfCharacterFromSet(characterSet) != nil {
if let number = Int(substr) {
// Is a number
let nsNumber = NSNumber(integer: number)
spelledOutComponents.append(numberFormatter.stringFromNumber(nsNumber)!)
}
} else {
// Is not a number
spelledOutComponents.append(substring!)
}
}
// Finally separate the components with spaces (so that the string doesn't become "ninefivesixfive".
return spelledOutComponents.joinWithSeparator(" ")
}