美文网首页
关于TextView

关于TextView

作者: 楠Y | 来源:发表于2017-03-20 16:24 被阅读8次

    设置文字内边距

    self.textView.textContainerInset = UIEdgeInsetsMake(10, 10, 0, 10);

    设置placeholder

    .h

    #import@interface FEPlaceHolderTextView : UITextView

    @property (nonatomic, retain) NSString *placeholder;

    @property (nonatomic, retain) UIColor *placeholderColor;

    -(void)textChanged:(NSNotification*)notification;

    @end

    .m

    #import "FEPlaceHolderTextView.h"

    @interface FEPlaceHolderTextView ()

    @property (nonatomic, retain) UILabel *placeHolderLabel;

    @end

    @implementation FEPlaceHolderTextView

    CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;

    - (void)dealloc

    {

    [[NSNotificationCenter defaultCenter] removeObserver:self];

    }

    - (void)awakeFromNib

    {

    [super awakeFromNib];

    if (!self.placeholder) {

    [self setPlaceholder:@""];

    }

    if (!self.placeholderColor) {

    [self setPlaceholderColor:[UIColor lightGrayColor]];

    }

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

    }

    - (id)initWithFrame:(CGRect)frame

    {

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

    {

    [self setPlaceholder:@""];

    [self setPlaceholderColor:[UIColor lightGrayColor]];

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

    }

    return self;

    }

    - (void)textChanged:(NSNotification *)notification

    {

    if([[self placeholder] length] == 0)

    {

    return;

    }

    [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{

    if([[self text] length] == 0)

    {

    [[self viewWithTag:999] setAlpha:1];

    }

    else

    {

    [[self viewWithTag:999] setAlpha:0];

    }

    }];

    }

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

    [super setText:text];

    [self textChanged:nil];

    }

    - (void)drawRect:(CGRect)rect

    {

    if( [[self placeholder] length] > 0 )

    {

    if (_placeHolderLabel == nil )

    {

    _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width,10)];

    _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;

    _placeHolderLabel.numberOfLines =0;

    _placeHolderLabel.font = self.font;

    _placeHolderLabel.backgroundColor = [UIColor clearColor];

    _placeHolderLabel.textColor = self.placeholderColor;

    _placeHolderLabel.alpha = 0;

    _placeHolderLabel.tag = 999;

    [self addSubview:_placeHolderLabel];

    }

    _placeHolderLabel.text = self.placeholder;

    [_placeHolderLabel sizeToFit];

    [self sendSubviewToBack:_placeHolderLabel];

    }

    if( [[self text] length] == 0 && [[self placeholder] length] > 0 )

    {

    [[self viewWithTag:999] setAlpha:1];

    }

    [super drawRect:rect];

    }

    @end

    调用:

    self.textView=[[FEPlaceHolderTextView alloc]initWithFrame:CGRectMake(10, 30, CGRectGetWidth(self.view.frame)-20, 200)];

    self.textView.placeholder=@"博客园FlyElephant\n博客地址:http://www.cnblogs.com/xiaofeixiang";

    self.textView.layer.borderColor=[UIColor lightGrayColor].CGColor;

    self.textView.layer.borderWidth=1.0;

    self.textView.scrollEnabled = YES;

    self.textView.autoresizingMask =

    UIViewAutoresizingFlexibleHeight; //自适应高度

    self.textView.returnKeyType = UIReturnKeyDefault; //返回键的类型

    self.textView.keyboardType = UIKeyboardTypeDefault; //键盘类型

    [self.view addSubview:self.textView];

    相关文章

      网友评论

          本文标题:关于TextView

          本文链接:https://www.haomeiwen.com/subject/ujhinttx.html