TextKit學(xué)習(xí)(三)NSTextStorage椅棺,NSLayoutManager护昧,NSTextContainer和UITextView
解決NSTextContainer分頁時(shí)文本截?cái)鄦栴}
iOS 簡易文本控件開發(fā)(UIKeyInput協(xié)議學(xué)習(xí))
textView
//實(shí)例一
原始: _textView.contentSize :{375, 659}
_textView.text = nil;
做以下設(shè)置墓阀。展示結(jié)果
1.
_textView.contentInset = UIEdgeInsetsMake(332, 0, 320, 0)塌碌;
NSLog _textView.contentSize : {375, 659}
說明contentInset 不會(huì)增加contentSize。
2.
_textView.textContainerInset = UIEdgeInsetsMake(332, 0, 320, 0);
NSLog _textView.contentSize : {375, 669}
說明textContainerInset 增加contentSize蔬浙。
//實(shí)例二
原始: _textView.contentSize :{375, 659}
_textView.text = @"fdfdf";
做以下設(shè)置猪落。展示結(jié)果
1.
_textView.contentInset = UIEdgeInsetsMake(332, 0, 320, 0);
NSLog _textView.contentSize : {375, 33}
說明contentInset 不會(huì)增加contentSize畴博。
2.
_textView.textContainerInset = UIEdgeInsetsMake(332, 0, 320, 0);
NSLog _textView.contentSize : {375, 669}
說明textContainerInset 增加contentSize笨忌。
一 、設(shè)置文本內(nèi)邊距:top,left,top使用
@property(nonatomic, assign) UIEdgeInsets textContainerInset
設(shè)置bottom用@property(nonatomic, assign) UIEdgeInsets contentInset
//將textView的左右邊距都設(shè)置成 0俱病;contentInset 設(shè)置右邊距是沒有用的官疲。
UIEdgeInsets insets = _textView.textContainerInset;
insets.left = - 4;
insets.right = -4;
_textView.textContainerInset = insets;
//textContainerInset 設(shè)置下邊距是沒有用的。得用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,這樣會(huì)根據(jù)有無內(nèi)容確認(rèn)按鈕是否變灰
UITextField只限中文途凫、英文、數(shù)字輸入和限制字符個(gè)數(shù)的實(shí)現(xiàn)方法
1.borderStyle為空的時(shí)候溢吻,光標(biāo)才會(huì)在最左邊维费。
鍵盤通知
- (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];
// 沒有高亮選擇的字,則對已輸入的漢字進(jìn)行字?jǐn)?shù)統(tǒng)計(jì)和限制
if (!position)
{
if (toBeString.length > 5) {
_textField.text = [toBeString substringToIndex:5];
}
}
// 對高亮文本不做限制促王,因?yàn)樗皇亲罱K顯示在輸入框的文本犀盟。
else
{
}
}
// 中文輸入法以外的直接對其統(tǒng)計(jì)限制即可,不考慮其他語種情況
else
{
//有一種情況沒考慮蝇狼,就是imoji是占兩個(gè)字符的阅畴,輸入3個(gè)imoji,最后一個(gè)顯示不全
if (toBeString.length > 5) {
_textField.text = [toBeString substringToIndex:5];
}
}
}
禁止長按出現(xiàn)選擇迅耘,拷貝贱枣,粘貼
-(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];
}
禁止自動(dòng)聯(lián)想(聯(lián)想會(huì)在上方添加一個(gè)工具條顯示輸入的內(nèi)容)
autocorrectionType = UITextAutocorrectionTypeNo
輸入內(nèi)容限制(限制輸入字母和數(shù)字)
1774807-9ab5b00ea0ace17d.png.jpeg
限制輸入長度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ( textField.text.length >= _count) {
return NO;
}
return YES;
}
鍵盤類型
UIKeyboardTypeASCIICapable: 字母為主,數(shù)字可以雕出
UIKeyboardTypeNumbersAndPunctuation:數(shù)字和標(biāo)點(diǎn)
UIKeyboardTypeURL:輸入url
UIKeyboardTypeNumberPad:只有數(shù)字
UIKeyboardTypePhonePad:電話鍵盤(數(shù)字颤专、+*#)
UIKeyboardTypeEmailAddress:郵箱鍵盤
UIKeyboardTypeDecimalPad:小數(shù)鍵盤(比數(shù)字鍵盤多一個(gè)小數(shù)點(diǎn))