国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

iOS-自定義TextView的方法,可以設(shè)置占位文字(placeholder)又可滾動(dòng)

2019-11-14 18:28:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

自定義TextView

在使用textView的時(shí)候,我們?nèi)绻M鼡碛衪extField的占位文字的功能,就要自定義了。

先看自定義的流程:

下面給出具體的代碼:

(后面有注意點(diǎn)講解)

//.h文件#import <UIKit/UIKit.h>@interface XYLPlaceHodlerTextView : UITextView/**placeholder占位文字*/@PRoperty (nonatomic, copy) NSString *placeholder;/**placeholderColor占位文字顏色*/@property (nonatomic, strong) UIColor *placeholderColor;@end
// .m文件#import "XYLPlaceHodlerTextView.h"@interface XYLPlaceHodlerTextView()/**UILabel*/@property (nonatomic, strong) UILabel *placeholderLabel;@end@implementation XYLPlaceHodlerTextView/** *  懶加載屬性,并設(shè)置屬性的值 */-(UILabel *)placeholderLabel{    if (!_placeholderLabel) {        UILabel *label = [[UILabel alloc]init];        label.font = [UIFont systemFontOfSize:14];        label.textColor = [UIColor grayColor];        label.numberOfLines = 0;        self.placeholderLabel = label;    }    return self.placeholderLabel;}/** *  設(shè)置自己的屬性 */-(instancetype)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {        self.alwaysBounceVertical = YES;        self.textColor = [UIColor blackColor];        [XYLNotificationCenter addObserver:self selector:@selector(texting) name:UITextViewTextDidChangeNotification object:self];    }    return self;}/** *  監(jiān)聽(tīng)有文字輸入 */-(void)texting{    [self setPlaceholderTextShow];}/** *  設(shè)置占位文字的顯示 */-(void)setPlaceholderTextShow{    self.placeholderLabel.hidden = self.hasText;}-(void)layoutSubviews{    [super layoutSubviews];    self.placeholderLabel.x = 4;    self.placeholderLabel.y = 8;    self.placeholderLabel.width = self.width - 2 * self.placeholderLabel.x;    [self.placeholderLabel sizeToFit];//這一步很重要,不能遺忘}-(void)setPlaceholder:(NSString *)placeholder{//    _placeholder = placeholder;//此句的意義何在?    self.placeholderLabel.text = placeholder;    [self setNeedsLayout];}-(void)setPlaceholderColor:(UIColor *)placeholderColor{    self.placeholderLabel.textColor = placeholderColor;    [self setNeedsLayout];}-(void)setFont:(UIFont *)font{    [super setFont:font];    self.placeholderLabel.font = font;    [self setNeedsLayout];}-(void)setText:(NSString *)text{    [super setText:text];    [self setPlaceholderTextShow];}-(void)setAttributedText:(NSAttributedString *)attributedText{    [super setAttributedText:attributedText];    [self setPlaceholderTextShow];}@end

注意點(diǎn):

界面顯示步驟是先調(diào)用layoutSubviews將所有子控件的位置frame設(shè)計(jì)好,然后再調(diào)用drawRect方法畫(huà)上去。

layoutSubviews的調(diào)用時(shí)機(jī):

  • init時(shí)不會(huì)被調(diào)用

  • 將要真正顯示的會(huì)調(diào)用

  • frame發(fā)現(xiàn)改變時(shí)智能調(diào)用

  • 滾動(dòng)、旋轉(zhuǎn)、remove等等時(shí)

  • 這些時(shí)機(jī)都是和frame相關(guān)的,也是唯一能更新子視圖的最好時(shí)機(jī)


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜君县| 武城县| 临泽县| 玉溪市| 明星| 清镇市| 红安县| 肇庆市| 上饶市| 板桥市| 汝州市| 岑溪市| 沙坪坝区| 通州市| 翼城县| 石楼县| 满城县| 玉田县| 修文县| 应用必备| 宜宾市| 平遥县| 黎平县| 新民市| 花垣县| 岫岩| 信丰县| 兴业县| 哈密市| 台南市| 油尖旺区| 海兴县| 黄大仙区| 罗江县| 巴东县| SHOW| 合肥市| 崇州市| 黄龙县| 潞城市| 辽宁省|