现在iOS还没有想h5那样支持placeholder,所以我们只能在UITextArea上添加一层UILabel,首先你需要在该类中实现
UITextViewDelegate监听
UITextView
descriptionTextView
= [[
UITextView
alloc
]
initWithFrame
:
CGRectMake
(
5
, 5
,
descriptionView
.
frame
.
size
.
width
-
5
,
textHeight
)];
descriptionTextView.delegate = self;
[
descriptionView
addSubview
:
descriptionTextView
];
//placeholder 用 uilabel 显示
//placeholder 用 uilabel 显示
placeHolder = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, 70, textHeight)];
placeHolder
.
enabled
=
NO
;
placeHolder . text = @" 请输入 " ;
placeHolder . font = kFont_14 ;
placeHolder . textColor =[ UIColor grayColor ];
placeHolder . text = @" 请输入 " ;
placeHolder . font = kFont_14 ;
placeHolder . textColor =[ UIColor grayColor ];
[descriptionView addSubview:placeHolder];
实现的代理的方法:
//placeholder
监听
-( void )textViewDidChange:( UITextView *)textView
{
CGRect temp;
if ( descriptionTextView . text . length == 0 ) {
placeHolder . text = @" 请输入 " ;
// 改变 textView 的高度
CGRect temp = descriptionTextView . frame ;
temp. size . height = textHeight ;
descriptionTextView . frame = temp;
} else {
placeHolder . text = @"" ;
// 改变 textView 的高度
CGRect temp = descriptionTextView . frame ;
temp. size . height = textView. contentSize . height ;
descriptionTextView . frame = temp;
}
// 根据 textview 的字数改变 textview 的 frame
// 联动下面的 view
temp = button . frame ;
temp. origin . y = CGRectGetMaxY ( descriptionTextView . frame )+ 5 ;
-( void )textViewDidChange:( UITextView *)textView
{
CGRect temp;
if ( descriptionTextView . text . length == 0 ) {
placeHolder . text = @" 请输入 " ;
// 改变 textView 的高度
CGRect temp = descriptionTextView . frame ;
temp. size . height = textHeight ;
descriptionTextView . frame = temp;
} else {
placeHolder . text = @"" ;
// 改变 textView 的高度
CGRect temp = descriptionTextView . frame ;
temp. size . height = textView. contentSize . height ;
descriptionTextView . frame = temp;
}
// 根据 textview 的字数改变 textview 的 frame
// 联动下面的 view
temp = button . frame ;
temp. origin . y = CGRectGetMaxY ( descriptionTextView . frame )+ 5 ;
button.frame = temp;
}