
讀書無用論的去死.jpg
前言
通過Category一行代碼實現(xiàn)UITextView的placeHolder。搭配使用一行代碼限制并統(tǒng)計UITextView輸入字?jǐn)?shù)效果更佳。
實現(xiàn)效果

ZWPlaceHolder+LimitCouner.gif
導(dǎo)入頭文件
#import "UITextView+ZWPlaceHolder.h"
- storyboard(xib)一行調(diào)用代碼
self.firstTextView.zw_placeHolder = @"向廠家反饋同業(yè)相關(guān)活動、產(chǎn)品信息、用于市場分析。";
- 代碼創(chuàng)建UITextView調(diào)用
CGRect rect = CGRectMake(5, 230, [UIScreen mainScreen].bounds.size.width-10, 80);
UITextView *textView = [[UITextView alloc] initWithFrame:rect];
textView.layer.borderWidth = 1;
textView.font = [UIFont systemFontOfSize:14];
textView.layer.borderColor = [UIColor lightGrayColor].CGColor;
textView.zw_placeHolder = @"向廠家反饋同業(yè)相關(guān)活動、產(chǎn)品信息、用于市場分析。";
[self.view addSubview:textView];
- 調(diào)整placeHolder的字體大小
- placeholder的字體大小會跟隨UITextView字體大小而變化,可以通過調(diào)整UITextView的font來調(diào)整PlaceHolder字體大小
textView.font = [UIFont systemFontOfSize:14];
- 調(diào)整placeHolder的字體顏色
textView.zw_placeHolderColor = [UIColor redColor];

調(diào)整placeHolder字體顏色.png
實現(xiàn)源碼解析
- 在UITextView的category中通過runtime添加屬性zw_placeHolder,當(dāng)給zw_placeHolder賦值時,去創(chuàng)建一個UILabel添加到UITextView上,然后監(jiān)聽UITextView中text的改變來選擇是否顯示placeHolder。
- 核心代碼
#pragma mark - update
- (void)updatePlaceHolder{
if (self.text.length) {
[self.zw_placeHolderLabel removeFromSuperview];
return;
}
self.zw_placeHolderLabel.font = self.font?self.font:self.cacutDefaultFont;
self.zw_placeHolderLabel.textAlignment = self.textAlignment;
self.zw_placeHolderLabel.text = self.zw_placeHolder;
[self insertSubview:self.zw_placeHolderLabel atIndex:0];
}
#pragma mark - lazzing
-(UILabel *)zw_placeHolderLabel{
UILabel *placeHolderLab = objc_getAssociatedObject(self, @selector(zw_placeHolderLabel));
if (!placeHolderLab) {
placeHolderLab = [[UILabel alloc] init];
placeHolderLab.numberOfLines = 0;
placeHolderLab.textColor = [UIColor lightGrayColor];
objc_setAssociatedObject(self, @selector(zw_placeHolderLabel), placeHolderLab, OBJC_ASSOCIATION_RETAIN);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePlaceHolder) name:UITextViewTextDidChangeNotification object:self];
}
return placeHolderLab;
}
- 如何避免placeHolder被光標(biāo)遮擋。
UITextView的光標(biāo)默認(rèn)邊距是5.0。
// Default value: 5.0 The layout padding at the beginning and end of the line fragment rects insetting the layout width available for the contents. This value is utilized by NSLayoutManager for determining the layout width.
@property(NS_NONATOMIC_IOSONLY) CGFloat lineFragmentPadding;
- 所以計算自定義placeHolder的位置需要加上這個邊距
- 關(guān)于UITextView的textContainerInset導(dǎo)致文字偏移
- UITextView可以通過設(shè)置textContainerInset來讓文字輸入?yún)^(qū)域偏移。
CGFloat x = lineFragmentPadding + textContainerInset.left + self.layer.borderWidth;
placeHolder的x坐標(biāo) = 光標(biāo)偏移量+text偏移量+border邊框?qū)挾取?br> 其他坐標(biāo)同理算得。
如何使用
- cocoapods導(dǎo)入(搜索不到請更新本地倉庫)
pod 'ZWPlaceHolder'
- 直接將文件拖入工程中,引入頭文件即可
#import "UITextView+ZWPlaceHolder.h"
搭配使用
- 參考上一篇一行代碼限制并統(tǒng)計UITextView輸入字?jǐn)?shù)
搭配.gif
源碼
- 源碼放在GitHub上,歡迎指正,記得star哦!
cocoapod版本更新記錄
- 0.0.2 ---2017-09-14
- 修改當(dāng)設(shè)置placeHolder后給UITextView的Text賦值,導(dǎo)致文字覆蓋在placeHolder上的bug.
- 添加和UITextField統(tǒng)一的placeholder屬性、為部分第三方鍵盤框架提供支持。例如:IQKeyboardManager會讀取placeholder屬性并創(chuàng)建UIToolbar.
