Swift学习记录

1.实现输入手机号码间隔显示
手机号码间隔显示
  @objc func transformToPhoneNumber() {
        let textStr = textField.text
        var termStr = textStr?.replacingOccurrences(of: " ", with: "")
        if (termStr?.characters.count)! < 12 {
            if (termStr?.characters.count)! > 7 {
                let secondIndex = termStr?.index((termStr?.startIndex)!, offsetBy: 7)
                termStr?.insert(" ", at: secondIndex!)
            }
            if (termStr?.characters.count)! > 3 {
                let firstIndex = termStr?.index((termStr?.startIndex)!, offsetBy: 3)
                termStr?.insert(" ", at: firstIndex!)
            }
        }
        textField.text = termStr
        print(termStr!)
  }

你可能感兴趣的:(Swift学习记录)