Swift 2.0 扩展TextView的placeHolder属性

//

//TextView+Extension.swift

//Tiger

//

//Created by admin on 15/12/5.

//Copyright © 2015年xidaMM. All rights reserved.

//

importFoundation

//MARK:扩展TextView的placeHolder属性

varkPlaceholderLabelPointer:UInt8=0

extension UITextView{

var placeHolderTextView:UITextView? {

get{

return objc_getAssociatedObject(self,&kPlaceholderLabelPointer)as?UITextView

}

set{

objc_setAssociatedObject(self, &kPlaceholderLabelPointer, newValue,objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)

}

}

funcsetPlaceHolder(placeHolderStr:String) {

ifplaceHolderTextView==nil{

placeHolderTextView=UITextView(frame:self.bounds)

placeHolderTextView?.userInteractionEnabled=false

placeHolderTextView?.text= placeHolderStr

placeHolderTextView?.textColor=UIColor.grayColor()

self.insertSubview(placeHolderTextView!, atIndex:0)

}

}

publicoverridefuncwillMoveToSuperview(newSuperview:UIView?) {

NSNotificationCenter.defaultCenter().addObserver(self,

selector:Selector("_didChange:"),

name:UITextViewTextDidChangeNotification,

object:nil)

}

func_didChange (notification:NSNotification) {

guardplaceHolderTextView!=nilelse{

return

}

ifnotification.object===self{

iftext.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) >0{

placeHolderTextView!.hidden=true

}else{

placeHolderTextView!.hidden=false

}

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容