Voice over doesn't read phone number properly

后端 未结 5 1179
半阙折子戏
半阙折子戏 2020-12-17 03:36

I have phone number in below format

1-1xx-2xx-9565

Currently VO read it as \"One (pause) One x x

5条回答
  •  旧巷少年郎
    2020-12-17 04:02

    Here is the code in 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(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(" ")
    }
    

提交回复
热议问题