Advertisement
lvs

Textfield class

lvs
Feb 5th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. function _M.newTextField(params)
  2.     local group = params.group
  3.     local back = display.newImageRect(group, 'images/login_field.png', 281, 36)
  4.     game:setRP(back, 'TopCenter')
  5.     local x = params.x or 0
  6.     back.x, back.y = x, params.y
  7.     local text = params.text or ''
  8.     --local title = _M.newLabel{text = text, x = x + -132, y = params.y + 14, font = native.systemFont, size = 22, color = _COLORS.black, align = 'CenterLeft', flat = true}
  9.     local title = _M.newLabel{text = text, x = x + -132, y = params.y + 20, font = _FONTNAME_HEAVY_CONDENSED, size = 22, color = _COLORS.black, align = 'CenterLeft', flat = true}
  10.     group:insert(title)
  11.     back.title = title
  12.    
  13.     function back:touch(event)
  14.         if event.phase == 'began' then
  15.             if type(params.onclick) == 'function' then
  16.                 params.onclick(self, 'up')
  17.             else
  18.                 self:showTextField()
  19.             end
  20.         end
  21.         return true
  22.     end
  23.     back:addEventListener('touch', back)
  24.    
  25.     function back:showTextField()
  26.         if self.title.isVisible then
  27.             self.title.isVisible = false
  28.             local w, h = 260, 28
  29.             local x, y = back:localToContent(0, 0)
  30.             self.textField = native.newTextField(x - 132, y - 13, w, h)
  31.             if self.current_text and self.current_text ~= params.title then
  32.                 self.textField.text = self.current_text
  33.             end
  34.             if params.password then
  35.                 self.textField.isSecure = params.password
  36.             end
  37.             self.textField.hasBackground = false
  38.             self.textField.size = 22--math.floor(22 * display.pixelWidth / (_R - _L))
  39.             self.textField.font = native.newFont(_FONTNAME_HEAVY_CONDENSED, 22)
  40.             if params.mode then
  41.                 self.textField.inputType = params.mode
  42.             end
  43.             if params.userInput then
  44.                 self.textField:addEventListener('userInput', params.userInput)
  45.             end
  46.             native.setKeyboardFocus(self.textField)
  47.         end
  48.     end
  49.    
  50.     function back:hideTextField()
  51.         if not self.title.isVisible then
  52.             self.title.isVisible = true
  53.             self.current_text = self.textField.text:sub(1, 45)
  54.             self.title:setText(self.textField.text:sub(1, 45))
  55.             self.title:setTextColor(0, 0, 0)
  56.             if params.password then
  57.                 local s = string.gsub(self.title.text, '.', '●')
  58.                 self.title:setText(s)
  59.             end
  60.             native.setKeyboardFocus(nil)
  61.             if params.userInput then
  62.                 self.textField:removeEventListener('userInput', params.userInput)
  63.             end
  64.             display.remove(self.textField)
  65.         end
  66.     end
  67.    
  68.     function back:checkEmpty()
  69.         if (self.textField and self.textField.text) or self.current_text then
  70.             local text = (self.textField and self.textField.text) or self.current_text
  71.             if text:len() > 5 then
  72.                 if params.mode == 'email' then
  73.                     if text:match("[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?") then
  74.                         return true
  75.                     else
  76.                         native.showAlert(_APP_NAME, params.title .. ' is not valid.', {'OK'})
  77.                     end
  78.                 else
  79.                     return true
  80.                 end
  81.             else
  82.                 native.showAlert(_APP_NAME, params.title .. ' must be at least 6 characters long.', {'OK'})
  83.             end
  84.         else
  85.             native.showAlert(_APP_NAME, 'Please enter ' .. params.title, {'OK'})
  86.         end
  87.         return false
  88.     end
  89.    
  90.     function back:getText()
  91.         return (self.textField and self.textField.text) or self.current_text
  92.     end
  93.    
  94.     return back
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement