一、UITextView的基础使用详解
//初始化并定义大小
UITextView的代理方法如下:
//将要开始编辑
- (BOOL)textViewShouldBeginEditi
//将要结束编辑
- (BOOL)textViewShouldEndEditing
//开始编辑
- (void)textViewDidBeginEditing:(UITextView *)textView;
//结束编辑
- (void)textViewDidEndEditing:(UITextView *)textView;
//内容将要发生改变编辑
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text;
//内容发生改变编辑
- (void)textViewDidChange:(UITextView *)textView;
//焦点发生改变
- (void)textViewDidChangeSelecti
有时候我们要控件自适应输入的文本的内容的高度,只要在textViewDidChange的代理方法中加入调整控件大小的代理即可
- (void)textViewDidChange:(UITextView *)textView{
}
控制输入文字的长度和内容,可通调用以下代理方法实现
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
}
二、设置UITextView的行间距 & 行高等
1.如果只是静态显示textView的内容为设置的行间距,执行如下代码:
// textview 改变字体的行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 10;// 字体的行间距
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:15],
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:@"输入你的内容" attributes:attributes];
2.如果是想在输入内容的时候就按照设置的行间距进行动态改变,那就需要将上面代码放到textView的delegate方法里
-(void)textViewDidChange:(UITextView *)textView
{
// textview 改变字体的行间距
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 20;// 字体的行间距
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:15],
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}
3.关于行高和文本缩进、铺满的定制
使用NSAttributeString进行定制
具体方法如下:
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
- paragraphStyle.lineHeightMultiple = 20.f;
- paragraphStyle.maximumLineHeight = 25.f;
- paragraphStyle.minimumLineHeight = 15.f;
- paragraphStyle.firstLineHeadIndent = 20.f;
- paragraphStyle.alignment = NSTextAlignmentJustified;
- NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:paragraphStyle, NSForegroundColorAttributeName:[UIColor colorWithRed:76./255. green:75./255. blue:71./255. alpha:1]
- };
- textView.attributedText = [[NSAttributedString alloc]initWithString:content attributes:attributes];
当然也可以初始化一个NSMutableAttributedString,然后向里面添加文字样式,最后将它赋给textView的AttributedText即可
- NSMutableAttributedString *atr = [[NSMutableAttributedString alloc]initWithString:detail];
- [atr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, detail.length)];
- textView.attributedText = atr;
另外,对于textview中的链接样式,同样也可以定制
- NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],
- NSUnderlineColorAttributeName: [UIColor blackColor],
- NSUnderlineStyleAttributeName: @(NSUnderlinePatternDash)};
- self.linkTextAttributes = linkAttributes;
三、设置UITextView的placeholder
UITextView上如何加上类似于UITextField的placeholder呢,其实在UITextView上加上一个UILabel或者UITextView,如果用UILable的话,会出现一个问题就是当placeholder的文字过长导致换行的时候就会出现问题,而用UITextView则可以有效避免此问题。
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{ if (![text isEqualToString:@""])
{
_placeholderLabel.hidden = YES;
}
if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
{
_placeholderLabel.hidden = NO;
}
return YES;
}
说明如下:
(1) _placeholderLabel 是加在UITextView后面的UITextView,_placeholderLabel要保证和真正的输入框的设置一样,字体设置成浅灰色,然后[_placeholderLabel setEditable:NO];真正的输入框要设置背景色透明,保证能看到底部的_placeholderLabel。
(2) [text isEqualToString:@""] 表示输入的是退格键
(3) range.location == 0 && range.length == 1 表示输入的是第一个字符