TextKit学习(三)NSTextStorage,NSLayoutManager,NSTextContainer和UITextView
解决NSTextContainer分页时文本截断问题
iOS 简易文本控件开发(UIKeyInput协议学习)
textView
//实例一
原始: _textView.contentSize :{375, 659}
_textView.text = nil;
做以下设置。展示结果
1.
_textView.contentInset = UIEdgeInsetsMake(332, 0, 320, 0);
NSLog _textView.contentSize : {375, 659}
说明contentInset 不会增加contentSize。
2.
_textView.textContainerInset = UIEdgeInsetsMake(332, 0, 320, 0);
NSLog _textView.contentSize : {375, 669}
说明textContainerInset 增加contentSize。
//实例二
原始: _textView.contentSize :{375, 659}
_textView.text = @"fdfdf";
做以下设置。展示结果
1.
_textView.contentInset = UIEdgeInsetsMake(332, 0, 320, 0);
NSLog _textView.contentSize : {375, 33}
说明contentInset 不会增加contentSize。
2.
_textView.textContainerInset = UIEdgeInsetsMake(332, 0, 320, 0);
NSLog _textView.contentSize : {375, 669}
说明textContainerInset 增加contentSize。
一 、设置文本内边距:top,left,top使用
@property(nonatomic, assign) UIEdgeInsets textContainerInset
设置bottom用@property(nonatomic, assign) UIEdgeInsets contentInset
//将textView的左右边距都设置成 0;contentInset 设置右边距是没有用的。
UIEdgeInsets insets = _textView.textContainerInset;
insets.left = - 4;
insets.right = -4;
_textView.textContainerInset = insets;
//textContainerInset 设置下边距是没有用的。得用contentInset
UIEdgeInsets insets = self.textView.contentInset;
insets.bottom = self.keyboardSpacingHeight;
self.textView.contentInset = insets;
二、占位文字颜色:
label.textColor = [UIColor colorWithRed:0 green: 0 blue:0 alpha:0.2];
textField
textField.enablesReturnKeyAutomatically = Yes,这样会根据有无内容确认按钮是否变灰
UITextField只限中文、英文、数字输入和限制字符个数的实现方法
1.borderStyle为空的时候,光标才会在最左边。
键盘通知
- (void)addNotification {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}
//键盘height获取
NSDictionary *info = notification.userInfo;
NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
限制输入长度
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)textFieldTextDidChange:(NSNotification *)notify {
NSString *toBeString = _textField.text;
NSString *lang = _textField.textInputMode.primaryLanguage; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) // 如果输入中文
{
UITextRange *selectedRange = [_textField markedTextRange];
//获取高亮部分
UITextPosition *position = [_textField positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的汉字进行字数统计和限制
if (!position)
{
if (toBeString.length > 5) {
_textField.text = [toBeString substringToIndex:5];
}
}
// 对高亮文本不做限制,因为它不是最终显示在输入框的文本。
else
{
}
}
// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
else
{
//有一种情况没考虑,就是imoji是占两个字符的,输入3个imoji,最后一个显示不全
if (toBeString.length > 5) {
_textField.text = [toBeString substringToIndex:5];
}
}
}
禁止长按出现选择,拷贝,粘贴
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if ([UIMenuController sharedMenuController]) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
//只是禁止拷贝
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:))
return NO;
return [super canPerformAction:action withSender:sender];
}
禁止自动联想(联想会在上方添加一个工具条显示输入的内容)
autocorrectionType = UITextAutocorrectionTypeNo
输入内容限制(限制输入字母和数字)
限制输入长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ( textField.text.length >= _count) {
return NO;
}
return YES;
}
键盘类型
UIKeyboardTypeASCIICapable: 字母为主,数字可以雕出
UIKeyboardTypeNumbersAndPunctuation:数字和标点
UIKeyboardTypeURL:输入url
UIKeyboardTypeNumberPad:只有数字
UIKeyboardTypePhonePad:电话键盘(数字、+*#)
UIKeyboardTypeEmailAddress:邮箱键盘
UIKeyboardTypeDecimalPad:小数键盘(比数字键盘多一个小数点)