uitextview Add placeholder (Swift)by Wusheying
add Uilabel and initialize
Public LetPlaceholderlabel:UILabel = UILabel()
@IBInspectable Publicvarplaceholder:String ="" {
Didset {
Placeholderlabel.text =placeholder
}
}
@IBInspectable PublicvarPlaceholdercolor:Uicolor =Uicolor(red:0.0, Green:0.0, Blue:0.0980392, Alpha:0.22) {
Didset {
Placeholderlabel.TextColor =Placeholdercolor
}
}
Override Publicvarfont:Uifont! {
Didset {
Placeholderlabel.Font =Font
}
}
Override Publicvartextalignment:nstextalignment {
Didset {
Placeholderlabel.TextAlignment =TextAlignment
}
}
Addsubview Addup
Override Init(frame:CGRect, TextContainer:Nstextcontainer?) {
Super.Init(Frame:frame, Textcontainer:textcontainer)
Commoninit()
}
Required PublicInit(coder Adecoder:Nscoder) {
Super.Init(Coder:adecoder)
Commoninit()
}
funcCommoninit () {
Nsnotificationcenter.Defaultcenter().Addobserver( Self,
selector:"Textdidchange",
Name:uitextviewtextdidchangenotification,
object:Nil)
Placeholderlabel.Font =Font
Placeholderlabel.TextColor =Placeholdercolor
Placeholderlabel.TextAlignment =TextAlignment
Placeholderlabel.text =placeholder
Placeholderlabel.NumberOfLines =0
Placeholderlabel.BackgroundColor =Uicolor.Clearcolor()
Placeholderlabel.settranslatesautoresizingmaskintoconstraints(false)
Addsubview(Placeholderlabel)
}
Enter the event that the text disappears
Override PublicvarText:String! {
Didset {
Textdidchange()
}
}
funcTextdidchange () {
Placeholderlabel.Hidden = !text.IsEmpty
}
Uitextview Add placeholder (Swift)