cocos2d-x lua简单封装一个EditBox,添加一个label放到EditBox上面用于放占位符,并进行各种设置,代码如下:
local NewEditBox = class("NewEditBox", function ( )
local editBoxSize = cc.size(0, 0)
return ccui.EditBox:create(editBoxSize,"LoginRegister/Login_tt.png")
end)
local editBox = nil--EditBox
PlaceHolderLabel = nil --占位符所在的label
PlaceHolderString = nil--占位符
PlaceHolderFontSize = nil--占位符字体大小
PlaceHolderFontName = nil --占位符字体名称
PlaceHolderColor = nil--占位符字体颜色
function NewEditBox:create( )
editBox = NewEditBox.new()
PlaceHolderLabel = cc.Label:create()
PlaceHolderLabel:addTo(editBox)
return editBox
end
function NewEditBox:setPlaceHolder( _placeHolderContent,_placeHolderFontSize,_placeHolderFontName,_placeHolderColor)
--获取到editBox的锚点及原点来设置占位符所在的label的位置信息
local ap = editBox:getAnchorPoint()
local px,py = editBox:getPosition()
local eSize = editBox:getContentSize()
--设置占位符所在label的位置信息
PlaceHolderLabel:setAnchorPoint(ap)
PlaceHolderLabel:setPosition(px,py)
PlaceHolderLabel:setDimensions(eSize.width, eSize.height)
--设置占位符对齐方式
PlaceHolderLabel:setAlignment(0,1)
--给label设置内容
PlaceHolderLabel:setString(_placeHolderContent)
PlaceHolderLabel:setTextColor(_placeHolderColor)
PlaceHolderLabel:setSystemFontSize(_placeHolderFontSize)
PlaceHolderLabel:setSystemFontName(_placeHolderFontName)
--赋值
PlaceHolderString = _placeHolderContent
PlaceHolderFontSize = _placeHolderFontSize
PlaceHolderFontName = _placeHolderFontName
PlaceHolderColor = _placeHolderColor
end
--设置占位符内容
function NewEditBox:setPlaceHolderString( _placeHolderContent )
-- body
PlaceHolderString = _placeHolderContent
PlaceHolderLabel:setString(_placeHolderContent)
end
--设置占位符的fontSize
function NewEditBox:setPlaceHolderFontSize( _placeHolderFontSize )
-- body
PlaceHolderFontSize = _placeHolderFontSize
PlaceHolderLabel:setSystemFontSize(_placeHolderFontSize)
end
--设置占位符的fontName
function NewEditBox:setPlaceHolderFontName( _placeHolderFontName )
-- body
PlaceHolderFontSize = _placeHolderFontName
PlaceHolderLabel:setSystemFontName(_placeHolderFontName)
end
--设置占位符的颜色
function NewEditBox:setPlaceHolderColor( _placeHolderColor )
-- body
PlaceHolderColor = _placeHolderColor
PlaceHolderLabel:setTextColor(_placeHolderColor)
end
--获取到占位符所在的label
function NewEditBox:getPlaceHolderLabel()
return PlaceHolderLabel
end
return NewEditBox