textView 與textField

TextKit學(xué)習(xí)(三)NSTextStorage,NSLayoutManager,NSTextContainer和UITextView
解決NSTextContainer分頁(yè)時(shí)文本截?cái)鄦?wèn)題

iOS 簡(jiǎn)易文本控件開(kāi)發(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}

說(shuō)明contentInset 不會(huì)增加contentSize。
2.
_textView.textContainerInset = UIEdgeInsetsMake(332, 0, 320, 0);
NSLog  _textView.contentSize : {375, 669}
說(shuō)明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}

說(shuō)明contentInset 不會(huì)增加contentSize。
2.
_textView.textContainerInset = UIEdgeInsetsMake(332, 0, 320, 0);
NSLog  _textView.contentSize : {375, 669}
說(shuō)明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è)置右邊距是沒(méi)有用的。
UIEdgeInsets insets = _textView.textContainerInset;
insets.left = - 4;
insets.right = -4;
_textView.textContainerInset = insets;
//textContainerInset 設(shè)置下邊距是沒(méi)有用的。得用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ù)有無(wú)內(nèi)容確認(rèn)按鈕是否變灰
UITextField只限中文、英文、數(shù)字輸入和限制字符個(gè)數(shù)的實(shí)現(xiàn)方法
1.borderStyle為空的時(shí)候,光標(biāo)才會(huì)在最左邊。

鍵盤(pán)通知

- (void)addNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}
//鍵盤(pán)height獲取
NSDictionary *info = notification.userInfo;
    NSValue *value = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
限制輸入長(zhǎng)度
- (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; // 鍵盤(pán)輸入模式
    if ([lang isEqualToString:@"zh-Hans"]) // 如果輸入中文
    {
        UITextRange *selectedRange = [_textField markedTextRange];
        //獲取高亮部分
        UITextPosition *position = [_textField positionFromPosition:selectedRange.start offset:0];
        // 沒(méi)有高亮選擇的字,則對(duì)已輸入的漢字進(jìn)行字?jǐn)?shù)統(tǒng)計(jì)和限制
        if (!position)
        {
            if (toBeString.length > 5) {
                _textField.text = [toBeString substringToIndex:5];
            }
        }
        // 對(duì)高亮文本不做限制,因?yàn)樗皇亲罱K顯示在輸入框的文本。
        else
        {
        }
    }
    // 中文輸入法以外的直接對(duì)其統(tǒng)計(jì)限制即可,不考慮其他語(yǔ)種情況
    else
    {
        //有一種情況沒(méi)考慮,就是imoji是占兩個(gè)字符的,輸入3個(gè)imoji,最后一個(gè)顯示不全
        if (toBeString.length > 5) {
            _textField.text = [toBeString substringToIndex:5];
        }
    }
    
}
禁止長(zhǎng)按出現(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
限制輸入長(zhǎng)度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ( textField.text.length >= _count) {
        return NO;
    }
    return YES;
}
鍵盤(pán)類(lèi)型
UIKeyboardTypeASCIICapable: 字母為主,數(shù)字可以雕出
UIKeyboardTypeNumbersAndPunctuation:數(shù)字和標(biāo)點(diǎn)
UIKeyboardTypeURL:輸入url
UIKeyboardTypeNumberPad:只有數(shù)字
UIKeyboardTypePhonePad:電話(huà)鍵盤(pán)(數(shù)字、+*#)
UIKeyboardTypeEmailAddress:郵箱鍵盤(pán)
UIKeyboardTypeDecimalPad:小數(shù)鍵盤(pán)(比數(shù)字鍵盤(pán)多一個(gè)小數(shù)點(diǎn))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容