Uitextview Add placeholder (Swift)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.