ios的uitextview怎么实现placehold

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios的uitextview怎么实现placehold相关的知识,希望对你有一定的参考价值。

参考技术A 使用UITextView经常会需要用到placeholder, 可惜UITextView没有提供这个功能, 那就开始动手写吧~

首先在创建一个CustomTextView来继承UITextView:

@interface CustomTextView : UITextView

@property (nonatomic, retain) NSString *placeholder;
@property (nonatomic, retain) UIColor *placeholderColor;

@end
加入两个property:
@property (nonatomic, retain) NSString *placeholder; //文字
@property (nonatomic, retain) UIColor *placeholderColor; //颜色
下一步就是具体实现了.m文件
先注册UITextViewTextDidChangeNotification来监听文字内容的改变, 初始化一些变量.

- (id)initWithFrame:(CGRect)frame

self = [super initWithFrame:frame];
if (self)

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:)
name:UITextViewTextDidChangeNotification
object:nil];

self.autoresizesSubviews = NO;
self.placeholder = @"";
self.placeholderColor = [UIColor lightGrayColor];


return self;

然后就是最核心的部分, 重写- (void)drawRect:(CGRect)rect
先确定好placeholder的位置(ios7中文字的位置和之前不一样, 所以placeholder也要对应调一下位置),设置好颜色,用NSString的drawInRect:绘制到TextView中.

- (void)drawRect:(CGRect)rect

//内容为空时才绘制placeholder
if ([self.text isEqualToString:@""])
CGRect placeholderRect;
placeholderRect.origin.y = 8;
placeholderRect.size.height = CGRectGetHeight(self.frame)-8;
if (IOS_VERSION >= 7)
placeholderRect.origin.x = 5;
placeholderRect.size.width = CGRectGetWidth(self.frame)-5;
else
placeholderRect.origin.x = 10;
placeholderRect.size.width = CGRectGetWidth(self.frame)-10;

[self.placeholderColor set];
[self.placeholder drawInRect:placeholderRect
withFont:self.font
lineBreakMode:NSLineBreakByWordWrapping
alignment:NSTextAlignmentLeft];


但- (void)drawRect:(CGRect)rect在self绘制或大小位置改变的时候被调用,我们输入文字是不会被调用的. 所以要调用self的setNeedsDisplay来重新绘制self里面的内容(placeholder).

- (void)textChanged:(NSNotification *)not

[self setNeedsDisplay];


- (void)setText:(NSString *)text

[super setText:text];
[self setNeedsDisplay];

一个简单的,带placeholder的TextView就搞定了.如果需要给placeholder设置字体或别的属性, 可以按照上面的思路实现.本回答被提问者和网友采纳

iOS中用UILabel实现UITextView的占位文字

 

@interface BSPublishTextView : UITextView

/** 对外属性占位字符 placeholder */

@property (nonatomic, copy) NSString *placeholder;

 

/** 对外属性占位符颜色 */

@property (nonatomic, strong) UIColor *placeholderColor;

@end

@interface BSPublishTextView ()

/** 占位字符 label */

@property (nonatomic, weak) UILabel *placeholderLabel;

@end

 

@implementation BSPublishTextView

- (UILabel *)placeholderLabel{

    if (!_placeholderLabel) {

        UILabel *placeholderLabel = [[UILabel alloc]init];

        placeholderLabel.backgroundColor = [UIColor greenColor];

        placeholderLabel.numberOfLines = 0;

        placeholderLabel.x = 4;

        placeholderLabel.y = 7;

        [self addSubview:placeholderLabel];

        _placeholderLabel = placeholderLabel;

    }

    return _placeholderLabel;

}

 

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // 永久垂直有弹簧效果

        self.alwaysBounceVertical = YES;

        self.font = [UIFont systemFontOfSize:15];

        self.placeholderColor = [UIColor grayColor];

     

        // 注册通知 当收到 UITextViewTextDidChangeNotification  textView文字改变时

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];    

    }

    return self;

}

 

//通知调用方法

- (void)textChange{

    self.placeholderLabel.hidden = self.hasText;

}

 

- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

 

- (void)layoutSubviews{

    [super layoutSubviews];

    //在 layoutSubviews 拿到 UITextView的宽度一定是正确的 ,这里如果外界即使更改了UITextView的宽度这里也是能算正确的

    //先设置placeholderLabel占位字符的宽度

     self.placeholderLabel.width = self.width - 2 * self.placeholderLabel.x;

 

    // placeholderLabel占位字符的大小size随里面的内容大小而变化

    [self.placeholderLabel sizeToFit];

}

 

// setNeedsDisplay方法 : 会在恰当的时刻自动调用drawRect:方法

// setNeedsLayout方法 : 会在恰当的时刻调用layoutSubviews方法

 

- (void)setPlaceholderColor:(UIColor *)placeholderColor{

    _placeholderColor = placeholderColor;

    self.placeholderLabel.textColor = placeholderColor;

}

 

- (void)setPlaceholder:(NSString *)placeholder{

    _placeholder = [placeholder copy];

    self.placeholderLabel.text = placeholder;

    [self setNeedsLayout]; //会在恰当的时刻调用layoutSubviews方法 这句代码可不写

}

 

- (void)setFont:(UIFont *)font{

    [super setFont:font];

    self.placeholderLabel.font = font;

    [self setNeedsLayout];  //会在恰当的时刻调用layoutSubviews方法 这句代码可不写

}

 

- (void)setText:(NSString *)text{

    [super setText:text];

    [self textChange];

}

 

- (void)setAttributedText:(NSAttributedString *)attributedText{

    [super setAttributedText:attributedText];

    [self textChange];

}

@end

 

以上是关于ios的uitextview怎么实现placehold的主要内容,如果未能解决你的问题,请参考以下文章

iOS中用UILabel实现UITextView的占位文字

ios uitextview 怎么设置编辑时弹出键盘

iOS 富文本编辑器 用原生UITextView实现

iOS7 UITextView contentsize.height替代方案

iOS UItextView监听输入特定字符跳转页面选择选项返回

iOS- UITextView与键盘回收与键盘遮挡输入框