關(guān)于TextView

設(shè)置文字內(nèi)邊距

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

設(shè)置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

調(diào)用:

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; //自適應(yīng)高度

self.textView.returnKeyType = UIReturnKeyDefault; //返回鍵的類(lèi)型

self.textView.keyboardType = UIKeyboardTypeDefault; //鍵盤(pán)類(lèi)型

[self.view addSubview:self.textView];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容