您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页Swift 2.0 扩展TextView的placeHolder

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

}

}

}

}

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务