iOSUITextView官方文档大总结

iOS-UITextView官方文档大总结

类继承

控件 父类 特性 独有的属性
UITextView (UIScrollview->UIView->UIResponder->NSObject) 可管理键盘 无预留字属性,可滚动,可展示多行,文字格式可自定义,
UITextfield (UIControl->UIView->UIResponder->NSObject) 可管理键盘 有预留字属性placeholder,只展示一行,

属性

attributedText

包括:font textcolor textAlignment
苹果推荐使用textview展示富文本 而不是webview.

管理键盘

涉及到的通知为:

  • UIKeyboardWillShowNotification
  • UIKeyboardDidShowNotification
  • UIKeyboardWillHideNotification
  • UIKeyboardDidHideNotification

键盘的外观可使用UITextInputTraits协议自定义.

初始化方法

-initWithFrame:textContainer:

1
2
3
textContainer是ios7之后新添加的属性,使用方法如下:
参考下篇博文: [如何求UILabel最后一个字符的Frame](http://www.jianshu.com/p/c26893bd0f48)

一般使用 -initWithFrame:即可.

配置text属性

editable = YES
editable = NO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
text
attributedText
font
textcolor
editable //A Boolean value indicating whether the receiver is editable.
/**
默认为YES, 置为NO后
如上图所示,
- 为YES时,可选择 可选择全部 可粘贴 既可以编辑,即其内容可以编辑改变;
- 为NO时 只可以复制 查询 (第三个不晓得是什么)共享,其内容不可改变.
**/
allowsEditingTextAttributes //A Boolean value indicating whether the receiver is editable. 默认为 NO
//识别网址链接 手机号码 邮箱
dataDetectorTypes//The types of data converted to tappable URLs in the text view.
/**
使用此属性时, editable = NO;否则不识别,即此时默认其内容不可被编辑 否则与 tap手势冲突,且一般用作展示作用.
**/
textAlignment
- hasText
typingAttributes //The attributes to apply to new text being entered by the user.默认左对齐.
linkTextAttributes//// 支持连接的样式.
/**
_textview.editable = NO;
_textview.dataDetectorTypes = UIDataDetectorTypeAll;
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineColorAttributeName: [UIColor blackColor],
NSUnderlineStyleAttributeName: @(NSUnderlinePatternDash)
};
_textview.linkTextAttributes = linkAttributes;
**/
textContainerInset

working With Selection

1
2
3
4
selectedRange
- scrollRangeToVisible:
clearsOnInsertion
selectable

Replacing the System Input Views

1
2
inputView
inputAccessoryView

Accessing Text Kit Objects

1
2
3
layoutManager
textContainer
textStorage

通知

1
2
3
UITextViewTextDidBeginEditingNotification
UITextViewTextDidChangeNotification
UITextViewTextDidEndEditingNotification

delegate

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
一 有关编辑的通知(Responding to Editing Notifications)
textViewShouldBeginEditing:
textViewDidBeginEditing:
textViewShouldEndEditing:
textViewDidEndEditing:
二是 有关text变化的代理(Responding to Text Changes)
textView: shouldChangeTextInRange: replacementText:
textViewDidChange:
三是 有关选择变化的通知 (Responding to Selection Changes)
textViewDidChangeSelection:
textView:shouldInteractWithTextAttachment:inRange:interaction:
//Asks the delegate if the specified text view should allow the specified type of user interaction with the provided text attachment in the given range of text.
Beta(待测)
textView:shouldInteractWithTextAttachment:inRange:
//Asks the delegate if the specified text view should allow user interaction with the provided text attachment in the given range of text.
Deprecated(已舍弃该方法)
textView:shouldInteractWithURL:inRange:interaction:
//Asks the delegate if the specified text view should allow the specified type of user interaction with the given URL in the given range of text.
Beta(待测)
textView:shouldInteractWithURL:inRange:
//Asks the delegate if the specified text view should allow user interaction with the given URL in the given range of text.
Deprecated(已舍弃该方法)

项目中有关textview常见的用法为:

实现类似预留字属性,和textfield一样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
初始化时
_placeholderLabel = [UILabel hdf_labelWithText:@"填写简介" textColor:kColorWith16RGB(0xafafaf) font:kFontWithSize(16) superView:_trainClassDescriptionTextView constraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(_trainClassDescriptionTextView).offset(10);
make.left.mas_equalTo(_trainClassDescriptionTextView).offset(5);
}];
代理方法中
- (void)textViewDidChange:(UITextView *)textView
{
if (textView.text.length <= 0) {
_placeholderLabel.hidden = NO;
}else
{
_placeholderLabel.hidden = YES;
}
}
实现预留字的方法参考:[自己写的](www.baidu.com).

实现最大输入字符数,同时提示不可输入特殊字符

此处有几个坑点:

  • 使用第三方输入法正常,但是使用系统输入法时(非九宫格时)输入拼音后还未选择点击中文时 其已开始计数,导致剩余的字符无法输入中文.
  • 修改了上述bug时,出现了使用系统输入法时,最后的字符可被同等数量的选择点击的英文字符所代替.
  • 使用系统九宫格输入法,获取到的text为其对应的数字键
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
- (void)textViewDidChange:(UITextView *)textView
{
if (textView.text.length <= 0) {
_placeholderLabel.hidden = NO;
}else
{
_placeholderLabel.hidden = YES;
}
**针对1的修改是 加入 textView.markedTextRange == nil这个判断
if (textView.markedTextRange == nil) {
NSString *str = [NSString stringWithFormat:@"%ld", (long)(90 - textView.text.length)];
if([str integerValue] >= 0 )
{
_lastFontNumLabel.text = str;
}else
{
_lastFontNumLabel.text = @"0";
}
if (_trainClassDescriptionTextView.text.length > kDescriptionMAXLength) // MAXLENGTH为最大字数
{
[HDFHud showTip:@"简介最多输入90个字"];
_trainClassDescriptionTextView.text = [_trainClassDescriptionTextView.text substringToIndex: kDescriptionMAXLength]; // MAXLENGTH为最大字数
}
}
}
**针对1的修改是 加入 textView.markedTextRange == nil这个判断
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if (textView.markedTextRange == nil) {
if (range.location >= 90) {
[HDFHud showTip:@"简介最多输入90个字"];
return NO;
}
**针对3的处理时 不在这里做拦截处理其输入的内容是否包含特殊符号,去掉下面的方法,因为使用系统九宫格输入法,获取到的text为其对应的数字键 比如输入m 时 此时 text为
//if([text isContainSpecialString])
//{
//[HDFHud showTip:@"不可包含特殊字符"];
// return NO;
//}
}
return YES;
}

textview实现富文本输入.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@property(NS_NONATOMIC_IOSONLY) CGFloat lineSpacing;
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacing;
@property(NS_NONATOMIC_IOSONLY) NSTextAlignment alignment;
@property(NS_NONATOMIC_IOSONLY) CGFloat firstLineHeadIndent;
@property(NS_NONATOMIC_IOSONLY) CGFloat headIndent;
@property(NS_NONATOMIC_IOSONLY) CGFloat tailIndent;
@property(NS_NONATOMIC_IOSONLY) NSLineBreakMode lineBreakMode;
@property(NS_NONATOMIC_IOSONLY) CGFloat minimumLineHeight;
@property(NS_NONATOMIC_IOSONLY) CGFloat maximumLineHeight;
@property(NS_NONATOMIC_IOSONLY) NSWritingDirection baseWritingDirection;
@property(NS_NONATOMIC_IOSONLY) CGFloat lineHeightMultiple;
@property(NS_NONATOMIC_IOSONLY) CGFloat paragraphSpacingBefore;
@property(NS_NONATOMIC_IOSONLY) float hyphenationFactor;
@property(null_resettable, copy, NS_NONATOMIC_IOSONLY) NSArray<NSTextTab *> *tabStops NS_AVAILABLE(10_0, 7_0);
@property(NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE(10_0, 7_0);
@property(NS_NONATOMIC_IOSONLY) BOOL allowsDefaultTighteningForTruncation NS_AVAILABLE(10_11, 9_0);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = thelineSpaceing;
// paragraphStyle.paragraphSpacing = 10.0f;
// paragraphStyle.paragraphSpacingBefore = 50.0f;
// paragraphStyle.alignment = NSTextAlignmentCenter;
// paragraphStyle.firstLineHeadIndent = 10.0f;
// paragraphStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
// paragraphStyle.headIndent = 30.0f;
// paragraphStyle.tailIndent = 0.1f;
// paragraphStyle.lineHeightMultiple = 2.0f;
// paragraphStyle.lineHeightMultiple = 10.0f;
// paragraphStyle.maximumLineHeight = 50.0f;
// paragraphStyle.minimumLineHeight = 50.0f;
NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName,theColor,NSForegroundColorAttributeName,nil];
NSMutableAttributedString *theAttributedString = [[NSMutableAttributedString alloc]initWithString:string attributes:tdic];
_textView.attributedText = theAttributedString;
坚持原创技术分享,您的支持将鼓励我继续创作!