FieldsRow.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // FieldsRow.swift
  2. // Eureka ( https://github.com/xmartlabs/Eureka )
  3. //
  4. // Copyright (c) 2016 Xmartlabs SRL ( http://xmartlabs.com )
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. import Foundation
  25. import UIKit
  26. open class TextCell: _FieldCell<String>, CellType {
  27. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  28. super.init(style: style, reuseIdentifier: reuseIdentifier)
  29. }
  30. required public init?(coder aDecoder: NSCoder) {
  31. super.init(coder: aDecoder)
  32. }
  33. open override func setup() {
  34. super.setup()
  35. textField.autocorrectionType = .default
  36. textField.autocapitalizationType = .sentences
  37. textField.keyboardType = .default
  38. }
  39. }
  40. open class IntCell: _FieldCell<Int>, CellType {
  41. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  42. super.init(style: style, reuseIdentifier: reuseIdentifier)
  43. }
  44. required public init?(coder aDecoder: NSCoder) {
  45. super.init(coder: aDecoder)
  46. }
  47. open override func setup() {
  48. super.setup()
  49. textField.autocorrectionType = .default
  50. textField.autocapitalizationType = .none
  51. textField.keyboardType = .numberPad
  52. }
  53. }
  54. open class PhoneCell: _FieldCell<String>, CellType {
  55. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  56. super.init(style: style, reuseIdentifier: reuseIdentifier)
  57. }
  58. required public init?(coder aDecoder: NSCoder) {
  59. super.init(coder: aDecoder)
  60. }
  61. open override func setup() {
  62. super.setup()
  63. textField.keyboardType = .phonePad
  64. if #available(iOS 10,*) {
  65. textField.textContentType = .telephoneNumber
  66. }
  67. }
  68. }
  69. open class NameCell: _FieldCell<String>, CellType {
  70. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  71. super.init(style: style, reuseIdentifier: reuseIdentifier)
  72. }
  73. required public init?(coder aDecoder: NSCoder) {
  74. super.init(coder: aDecoder)
  75. }
  76. open override func setup() {
  77. super.setup()
  78. textField.autocorrectionType = .no
  79. textField.autocapitalizationType = .words
  80. textField.keyboardType = .asciiCapable
  81. if #available(iOS 10,*) {
  82. textField.textContentType = .name
  83. }
  84. }
  85. }
  86. open class EmailCell: _FieldCell<String>, CellType {
  87. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  88. super.init(style: style, reuseIdentifier: reuseIdentifier)
  89. }
  90. required public init?(coder aDecoder: NSCoder) {
  91. super.init(coder: aDecoder)
  92. }
  93. open override func setup() {
  94. super.setup()
  95. textField.autocorrectionType = .no
  96. textField.autocapitalizationType = .none
  97. textField.keyboardType = .emailAddress
  98. if #available(iOS 10,*) {
  99. textField.textContentType = .emailAddress
  100. }
  101. }
  102. }
  103. open class PasswordCell: _FieldCell<String>, CellType {
  104. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  105. super.init(style: style, reuseIdentifier: reuseIdentifier)
  106. }
  107. required public init?(coder aDecoder: NSCoder) {
  108. super.init(coder: aDecoder)
  109. }
  110. open override func setup() {
  111. super.setup()
  112. textField.autocorrectionType = .no
  113. textField.autocapitalizationType = .none
  114. textField.keyboardType = .asciiCapable
  115. textField.isSecureTextEntry = true
  116. if #available(iOS 11,*) {
  117. textField.textContentType = .password
  118. }
  119. }
  120. }
  121. open class DecimalCell: _FieldCell<Double>, CellType {
  122. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  123. super.init(style: style, reuseIdentifier: reuseIdentifier)
  124. }
  125. required public init?(coder aDecoder: NSCoder) {
  126. super.init(coder: aDecoder)
  127. }
  128. open override func setup() {
  129. super.setup()
  130. textField.autocorrectionType = .no
  131. textField.keyboardType = .decimalPad
  132. }
  133. }
  134. open class URLCell: _FieldCell<URL>, CellType {
  135. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  136. super.init(style: style, reuseIdentifier: reuseIdentifier)
  137. }
  138. required public init?(coder aDecoder: NSCoder) {
  139. super.init(coder: aDecoder)
  140. }
  141. open override func setup() {
  142. super.setup()
  143. textField.autocorrectionType = .no
  144. textField.autocapitalizationType = .none
  145. textField.keyboardType = .URL
  146. if #available(iOS 10,*) {
  147. textField.textContentType = .URL
  148. }
  149. }
  150. }
  151. open class TwitterCell: _FieldCell<String>, CellType {
  152. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  153. super.init(style: style, reuseIdentifier: reuseIdentifier)
  154. }
  155. required public init?(coder aDecoder: NSCoder) {
  156. super.init(coder: aDecoder)
  157. }
  158. open override func setup() {
  159. super.setup()
  160. textField.autocorrectionType = .no
  161. textField.autocapitalizationType = .none
  162. textField.keyboardType = .twitter
  163. }
  164. }
  165. open class AccountCell: _FieldCell<String>, CellType {
  166. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  167. super.init(style: style, reuseIdentifier: reuseIdentifier)
  168. }
  169. required public init?(coder aDecoder: NSCoder) {
  170. super.init(coder: aDecoder)
  171. }
  172. open override func setup() {
  173. super.setup()
  174. textField.autocorrectionType = .no
  175. textField.autocapitalizationType = .none
  176. textField.keyboardType = .asciiCapable
  177. if #available(iOS 11,*) {
  178. textField.textContentType = .username
  179. }
  180. }
  181. }
  182. open class ZipCodeCell: _FieldCell<String>, CellType {
  183. required public init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  184. super.init(style: style, reuseIdentifier: reuseIdentifier)
  185. }
  186. required public init?(coder aDecoder: NSCoder) {
  187. super.init(coder: aDecoder)
  188. }
  189. open override func update() {
  190. super.update()
  191. textField.autocorrectionType = .no
  192. textField.autocapitalizationType = .allCharacters
  193. textField.keyboardType = .numbersAndPunctuation
  194. if #available(iOS 10,*) {
  195. textField.textContentType = .postalCode
  196. }
  197. }
  198. }
  199. open class _TextRow: FieldRow<TextCell> {
  200. public required init(tag: String?) {
  201. super.init(tag: tag)
  202. }
  203. }
  204. open class _IntRow: FieldRow<IntCell> {
  205. public required init(tag: String?) {
  206. super.init(tag: tag)
  207. let numberFormatter = NumberFormatter()
  208. numberFormatter.locale = Locale.current
  209. numberFormatter.numberStyle = .decimal
  210. numberFormatter.minimumFractionDigits = 0
  211. formatter = numberFormatter
  212. }
  213. }
  214. open class _PhoneRow: FieldRow<PhoneCell> {
  215. public required init(tag: String?) {
  216. super.init(tag: tag)
  217. }
  218. }
  219. open class _NameRow: FieldRow<NameCell> {
  220. public required init(tag: String?) {
  221. super.init(tag: tag)
  222. }
  223. }
  224. open class _EmailRow: FieldRow<EmailCell> {
  225. public required init(tag: String?) {
  226. super.init(tag: tag)
  227. }
  228. }
  229. open class _PasswordRow: FieldRow<PasswordCell> {
  230. public required init(tag: String?) {
  231. super.init(tag: tag)
  232. }
  233. }
  234. open class _DecimalRow: FieldRow<DecimalCell> {
  235. public required init(tag: String?) {
  236. super.init(tag: tag)
  237. let numberFormatter = NumberFormatter()
  238. numberFormatter.locale = Locale.current
  239. numberFormatter.numberStyle = .decimal
  240. numberFormatter.minimumFractionDigits = 2
  241. formatter = numberFormatter
  242. }
  243. }
  244. open class _URLRow: FieldRow<URLCell> {
  245. public required init(tag: String?) {
  246. super.init(tag: tag)
  247. }
  248. }
  249. open class _TwitterRow: FieldRow<TwitterCell> {
  250. public required init(tag: String?) {
  251. super.init(tag: tag)
  252. }
  253. }
  254. open class _AccountRow: FieldRow<AccountCell> {
  255. public required init(tag: String?) {
  256. super.init(tag: tag)
  257. }
  258. }
  259. open class _ZipCodeRow: FieldRow<ZipCodeCell> {
  260. public required init(tag: String?) {
  261. super.init(tag: tag)
  262. }
  263. }
  264. /// A String valued row where the user can enter arbitrary text.
  265. public final class TextRow: _TextRow, RowType {
  266. required public init(tag: String?) {
  267. super.init(tag: tag)
  268. }
  269. }
  270. /// A String valued row where the user can enter names. Biggest difference to TextRow is that it autocapitalization is set to Words.
  271. public final class NameRow: _NameRow, RowType {
  272. required public init(tag: String?) {
  273. super.init(tag: tag)
  274. }
  275. }
  276. /// A String valued row where the user can enter secure text.
  277. public final class PasswordRow: _PasswordRow, RowType {
  278. required public init(tag: String?) {
  279. super.init(tag: tag)
  280. }
  281. }
  282. /// A String valued row where the user can enter an email address.
  283. public final class EmailRow: _EmailRow, RowType {
  284. required public init(tag: String?) {
  285. super.init(tag: tag)
  286. }
  287. }
  288. /// A String valued row where the user can enter a twitter username.
  289. public final class TwitterRow: _TwitterRow, RowType {
  290. required public init(tag: String?) {
  291. super.init(tag: tag)
  292. }
  293. }
  294. /// A String valued row where the user can enter a simple account username.
  295. public final class AccountRow: _AccountRow, RowType {
  296. required public init(tag: String?) {
  297. super.init(tag: tag)
  298. }
  299. }
  300. /// A String valued row where the user can enter a zip code.
  301. public final class ZipCodeRow: _ZipCodeRow, RowType {
  302. required public init(tag: String?) {
  303. super.init(tag: tag)
  304. }
  305. }
  306. /// A row where the user can enter an integer number.
  307. public final class IntRow: _IntRow, RowType {
  308. required public init(tag: String?) {
  309. super.init(tag: tag)
  310. }
  311. }
  312. /// A row where the user can enter a decimal number.
  313. public final class DecimalRow: _DecimalRow, RowType {
  314. required public init(tag: String?) {
  315. super.init(tag: tag)
  316. }
  317. }
  318. /// A row where the user can enter an URL. The value of this row will be a URL.
  319. public final class URLRow: _URLRow, RowType {
  320. required public init(tag: String?) {
  321. super.init(tag: tag)
  322. }
  323. }
  324. /// A String valued row where the user can enter a phone number.
  325. public final class PhoneRow: _PhoneRow, RowType {
  326. required public init(tag: String?) {
  327. super.init(tag: tag)
  328. }
  329. }