Skip to content

Instantly share code, notes, and snippets.

@CassiusPacheco
Created June 5, 2018 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CassiusPacheco/02dadbe0043b5beeb1d239b252648827 to your computer and use it in GitHub Desktop.
Save CassiusPacheco/02dadbe0043b5beeb1d239b252648827 to your computer and use it in GitHub Desktop.
(iOS 11 compatible) UITextField extension to handle `textContentType` without dealing with iOS version checking.
//
// TextContentType.swift
// textfield
//
// Created by Cassius Pacheco on 5/6/18.
// Copyright © 2018 Cassius Pacheco. All rights reserved.
//
import UIKit
enum TextContentType {
case name
case namePrefix
case givenName
case middleName
case familyName
case nameSuffix
case nickname
case jobTitle
case organizationName
case location
case fullStreetAddress
case streetAddressLine1
case streetAddressLine2
case addressCity
case addressState
case addressCityAndState
case sublocality
case countryName
case postalCode
case telephoneNumber
case emailAddress
case URL
case creditCardNumber
case username
case password
case unavailable
@available(iOS 10.0, *)
var value: UITextContentType? {
switch self {
case .name:
return .name
case .namePrefix:
return .namePrefix
case .givenName:
return .givenName
case .middleName:
return .middleName
case .familyName:
return .familyName
case .nameSuffix:
return .nameSuffix
case .nickname:
return .nickname
case .jobTitle:
return .jobTitle
case .organizationName:
return .organizationName
case .location:
return .location
case .fullStreetAddress:
return .fullStreetAddress
case .streetAddressLine1:
return .streetAddressLine1
case .streetAddressLine2:
return .streetAddressLine2
case .addressCity:
return .addressCity
case .addressState:
return .addressState
case .addressCityAndState:
return .addressCityAndState
case .sublocality:
return .sublocality
case .countryName:
return .countryName
case .postalCode:
return .postalCode
case .telephoneNumber:
return .telephoneNumber
case .emailAddress:
return .emailAddress
case .URL:
return .URL
case .creditCardNumber:
return .creditCardNumber
case .username:
guard #available(iOS 11.0, *) else { return nil }
return .username
case .password:
guard #available(iOS 11.0, *) else { return nil }
return .password
case .unavailable:
return nil
}
}
@available(iOS 10.0, *)
init(type: UITextContentType) {
switch type {
case .name:
self = .name
case .namePrefix:
self = .namePrefix
case .givenName:
self = .givenName
case .middleName:
self = .middleName
case .familyName:
self = .familyName
case .nameSuffix:
self = .nameSuffix
case .nickname:
self = .nickname
case .jobTitle:
self = .jobTitle
case .organizationName:
self = .organizationName
case .location:
self = .location
case .fullStreetAddress:
self = .fullStreetAddress
case .streetAddressLine1:
self = .streetAddressLine1
case .streetAddressLine2:
self = .streetAddressLine2
case .addressCity:
self = .addressCity
case .addressState:
self = .addressState
case .addressCityAndState:
self = .addressCityAndState
case .sublocality:
self = .sublocality
case .countryName:
self = .countryName
case .postalCode:
self = .postalCode
case .telephoneNumber:
self = .telephoneNumber
case .emailAddress:
self = .emailAddress
case .URL:
self = .URL
case .creditCardNumber:
self = .creditCardNumber
default:
guard #available(iOS 11.0, *) else {
self = .unavailable
return
}
switch type {
case .password:
self = .password
case .username:
self = .username
default:
self = .unavailable
}
}
}
}
extension UITextField {
var textContentSafeType: TextContentType {
get {
guard #available(iOS 10.0, *), let type = self.textContentType else { return .unavailable }
return TextContentType(type: type)
}
set {
guard #available(iOS 10.0, *) else { return }
self.textContentType = newValue.value
}
}
func set(_ type: TextContentType, fallback: TextContentType) {
self.textContentSafeType = type
if self.textContentSafeType != type {
self.textContentSafeType = fallback
}
}
}
@CassiusPacheco
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment