日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

iOS 之UITextFiled/UITextView小结

發(fā)布時(shí)間:2023/12/19 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 之UITextFiled/UITextView小结 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一:編輯被鍵盤(pán)遮擋的問(wèn)題 ? ?(推薦使用第三方庫(kù):IQKeyboardManager ?用法參考

?參考自:http://blog.csdn.net/windkisshao/article/details/21398521

1.自定方法 ,用于移動(dòng)視圖

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval;

2.注冊(cè)監(jiān)聽(tīng)

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

? ? [defaultCenter?selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

? ? [defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

3.實(shí)現(xiàn)方法

- (void)keyboardWillShow:(NSNotification *)notification {

? ? NSDictionary *userInfo = [notification userInfo];

? ? NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

? ? CGRect keyboardRect = [aValue CGRectValue];

? ? NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

? ? NSTimeInterval animationDuration;

? ? [animationDurationValue getValue:&animationDuration];

? ? if(nil==self.myTextView) return;// ? ?self.editTextView 為被鍵盤(pán)遮擋住的控件

? ? CGRect rect = self.myTextView.frame;

? ? float textY = rect.origin.y + rect.size.height;?

? ? float bottomY = SCREENHEIGHT - textY;//得到下邊框到底部的距離 ?SCREENHEIGHT 為當(dāng)前設(shè)備的高度

? ? if(bottomY >=keyboardRect.size.height ){//鍵盤(pán)默認(rèn)高度,如果大于此高度,則直接返回

? ? ? ? return;

? ? }

? ? float moveY = keyboardRect.size.height - bottomY;

? ? [self moveInputBarWithKeyboardHeight:moveY withDuration:animationDuration];

}

?

- (void)keyboardWillHide:(NSNotification *)notification {

? ? NSDictionary* userInfo = [notification userInfo];

? ? NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

? ? NSTimeInterval animationDuration;

? ? [animationDurationValue getValue:&animationDuration];

? ? [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];

}

?

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval{

? CGRect rect1 = self.view.frame;

? ? [UIView beginAnimations:nil context:NULL];

? ? [UIView setAnimationDuration:_NSTimeInterval];

? ? rect1.origin.y = -_CGRectHeight;//view往上移動(dòng)

? ? self.view.frame = rect1;

? ? [UIView commitAnimations];

}

二:?鍵盤(pán)

(1)鍵盤(pán)類(lèi)型?

? ? ?UIKeyboardTypeDefault,?//?默認(rèn)鍵盤(pán):支持所有字符?

  • ?UIKeyboardTypeASCIICapable,?//?支持ASCII的默認(rèn)鍵盤(pán)?
  • ?UIKeyboardTypeNumbersAndPunctuation,?//?標(biāo)準(zhǔn)電話鍵盤(pán),支持+*#等符號(hào)?
  • ?UIKeyboardTypeURL,?//?URL鍵盤(pán),有.com按鈕;只支持URL字符?
  • ?UIKeyboardTypeNumberPad, ?//純數(shù)字鍵盤(pán) (不帶小數(shù)點(diǎn))
  • ?UIKeyboardTypeDecimalPad ?//數(shù)字鍵盤(pán)?(帶小數(shù)點(diǎn))
  • ?UIKeyboardTypePhonePad, ? //?電話鍵盤(pán)?
  • ?UIKeyboardTypeNamePhonePad,?//?電話鍵盤(pán),也支持輸入人名字?
  • ?UIKeyboardTypeEmailAddress,?//?用于輸入電子郵件地址的鍵盤(pán)
  • ?UIKeyboardTypeWebSearch ? ? //用于搜索
  • UIKeyboardTypeAlphabet
  • ? ?如:? self.uIphone.keyboardType = UIKeyboardTypeNumberPad;

    ? ?祥見(jiàn):http://blog.csdn.net/crazyzhang1990/article/details/39965931

    (2) return鍵的類(lèi)型?

    ? ? ? ?UIReturnKeyDefault, 默認(rèn) 灰色按鈕,標(biāo)有Return?

    ? ?  UIReturnKeyGo, ? ? 標(biāo)有Go的藍(lán)色按鈕? ? ? ? ? ?(完成,可用于填寫(xiě)資料的最后一項(xiàng))

    ?   ?UIReturnKeyGoogle,標(biāo)有Google的藍(lán)色按鈕,用語(yǔ)搜索?

    ?   ?UIReturnKeyJoin,標(biāo)有Join的藍(lán)色按鈕?

    ?  ? UIReturnKeyNext,標(biāo)有Next的藍(lán)色按鈕? ? ? ? ? ? ?(可用于登錄/注冊(cè)/填寫(xiě)地址-->下一步)

    ?  ? UIReturnKeyRoute,標(biāo)有Route的藍(lán)色按鈕?

    ?  ? UIReturnKeySearch,標(biāo)有Search的藍(lán)色按鈕? ? ?(可用于搜索)

    ?  ? UIReturnKeySend,標(biāo)有Send的藍(lán)色按鈕? ? ? ? ??

    ?   ?UIReturnKeyYahoo,標(biāo)有Yahoo的藍(lán)色按鈕?

    ?  ? UIReturnKeyYahoo,標(biāo)有Yahoo的藍(lán)色按鈕?

    ?  ? UIReturnKeyEmergencyCall, 緊急呼叫按鈕

    ? 如:

    ?self.uIphone.keyboardType = UIKeyboardTypeNumberPad;

    (3) 點(diǎn)擊return建響應(yīng)事件

    ? ?A.UITextField -->?- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    ? ?如: ?添加地址

    - (BOOL)textFieldShouldReturn:(UITextField *)textField{

    ? ? if(textField.tag==10)? //下一步 (姓名)

    ? ? {

    ? ? ? ? [self.uName resignFirstResponder];

    ? ? ? ? [self.uIphone becomeFirstResponder];

    ? ? }else if(textField.tag==11) ? //下一步 (電話)

    ? ? {

    ? ? ? ? [self.uIphone resignFirstResponder];

    ? ? ? ? [self.reciveAddress becomeFirstResponder];

    ? ? }else if(textField.tag==100) ? //完成(地址填完之后可直接調(diào)用接口)

    ? ? {

    ? ? ? ? [self.reciveAddress resignFirstResponder];

    ?? ? ? ? [self addBtn:nil];? ? ?

    ? ? }

    ? ? return YES;

    }

    B.UITextView -->?- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

    如:

    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    ????if ([text isEqualToString:@"\n"]){ //判斷輸入的字是否是回車(chē),即按下return
    ????????//在這里做你響應(yīng)return鍵的代碼

    ? ? ? ? ?[self addBtn:nil];

    ????????return NO; //這里返回NO,就代表return鍵值失效,即頁(yè)面上按下return,不會(huì)出現(xiàn)換行,如果為yes,則輸入頁(yè)面會(huì)換行
    ????}

    ????return YES;
    }

    參考自:http://blog.sina.com.cn/s/blog_59fb90df010176re.html

    ?

    三:UITextField小結(jié)

    ? ?1.自定義UITextField 左邊加圖標(biāo)(如:登錄)

    ? ? UIImageView *i=[[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 21, 21)];

    ? ? [i setImage:[UIImage imageNamed:@"yh"]];

    ? ? UIView *userLeftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 38)];

    ? ? [userLeftView addSubview:i];?

    ? ? self.userName.leftView=userLeftView;

    ? ? self.userName.leftViewMode=UITextFieldViewModeAlways;

    ? ? ?若只是單純的UITextField 縮進(jìn): 只需加 ?self.userName.leftViewMode=UITextFieldViewModeAlways;?self.userName.leftView=[[UIView alloc]init] 兩句即可。

    ? ?2.設(shè)置自定義UITextField 的Placeholder顏色? ?

    ? ?(1)方法一:

    ? ? ? ?UIColor *color = [UIColor blue];

    ? ? self.userName.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"? 手機(jī)號(hào)" ?attributes:@{NSForegroundColorAttributeName: color}];

    (2)方法二:? ? ? ??

    ? ?[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];?

    ?? [textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

    ? ?3.統(tǒng)一收起鍵盤(pán)? ? ??

    ? ? [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

    ? ? 4.?textFiled? 自動(dòng)換行? ? ? ? ? ?

    ? ? ? ? ??http://blog.csdn.net/u012661893/article/details/52183456

    四:UITextView小結(jié)

    ? 1.設(shè)置Placeholder? ? ??

    ? ? @property (nonatomic,strong) UILabel? *proText1;? ? ? ??

    ? ? self.automaticallyAdjustsScrollViewInsets = NO; ? ?

    ? ? [self.leaveMessage setDelegate:self];

    ? ? UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(self.leaveMessage.frame.origin.x+10, self.leaveMessage.frame.origin.y-35, self.leaveMessage.bounds.size.width, 100)];

    ? ? lbl.text=@" 感謝您留下寶貴的意見(jiàn)....";

    ? ? [lbl setFont:[UIFont systemFontOfSize:15.0]];

    ? ? lbl.enabled=NO;

    ? ? self.proText1=lbl;

    ? ? [self.view addSubview:lbl]; ? ?

    ? #pragma mark -----textView的代理事件

    ? -(void)textViewDidChange:(UITextView *)textView

    ? {

    ? ?//? textView.text = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    ? ? ? ? if (textView.text.length == 0) {

    ? ? ? ? ? ? self.proText1.text = @" 感謝您留下寶貴的意見(jiàn)....";

    ? ? ? ? }else{

    ? ? ? ? ? ? self.proText1.text = @"";

    ? ? ? ? }

    ? }

    2.去掉空格以及換行

    ?NSString *content = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    有的時(shí)候由于不正確的操作,直接將textView.text作為參數(shù)傳 到服務(wù)器,可能會(huì)產(chǎn)生意想不到的錯(cuò)誤,因此就需要加這個(gè)對(duì)字符串進(jìn)行一下處理

    ?

    3. 限制輸入長(zhǎng)度

    ? ??實(shí)現(xiàn)代理方法:

    - (void)textViewDidChange:(UITextView *)textView{

    ?? ? NSString *toBeString = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    ? ? //獲取高亮部分

    ? ? UITextRange *selectedRange = [textView markedTextRange];

    ? ? UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];

    ?? ?

    ? ? // 沒(méi)有高亮選擇的字,則對(duì)已輸入的文字進(jìn)行字?jǐn)?shù)統(tǒng)計(jì)和限制

    ? ? if (!position)

    ? ? {

    ? ? ? ? if (toBeString.length > MAX_STARWORDS_LENGTH)

    ? ? ? ? {

    ? ? ? ? ? ? [MBProgressHUD showAlert:@"最多只能輸入150個(gè)字。"];

    ? ? ? ? ? ? NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];

    ? ? ? ? ? ? if (rangeIndex.length == 1)

    ? ? ? ? ? ? {

    ? ? ? ? ? ? ? ? textView.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];

    ? ? ? ? ? ? }

    ? ? ? ? ? ? else

    ? ? ? ? ? ? {

    ? ? ? ? ? ? ? ? NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];

    ? ? ? ? ? ? ? ? textView.text = [toBeString substringWithRange:rangeRange];

    ? ? ? ? ? ? }

    ? ? ? ? }

    ? ? }

    }

    ?

    ?

    ?更多請(qǐng)參考:http://www.41443.com/HTML/iphone/20141109/204260.html

    ?

    補(bǔ)充:???

    1.長(zhǎng)按復(fù)制功能

    ?- (void)viewDidLoad {?

    ?? ? [self.view addGestureRecognizer:[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pasteBoard:)]];?

    }

    ?- (void)pasteBoard:(UILongPressGestureRecognizer *)longPress {?

    ? ? ? ? ? if (longPress.state == UIGestureRecognizerStateBegan) {

    ?? ? ? ? ? ? ? ? ? ? ? ? ? ? UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];?

    ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? pasteboard.string = @"需要復(fù)制的文本";

    ? ? ? ? ? } }

    ?

    2.導(dǎo)入自定義字體庫(kù)

    ? ? ?1、找到你想用的字體的 ttf 格式,拖入工程

    ?? ? 2、在工程的plist中增加一行數(shù)組,“Fonts provided by application”

    ?? ? 3、為這個(gè)key添加一個(gè)item,value為你剛才導(dǎo)入的ttf文件名

    ?? ? 4、直接使用即可:label.font = [UIFont fontWithName:@"你剛才導(dǎo)入的ttf文件名" size:20.0];

    ?附:計(jì)算文字的size

    /*** 計(jì)算一段文字size*/ - (CGSize)sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW {NSMutableDictionary *attrs = [NSMutableDictionary dictionary];attrs[NSFontAttributeName] = font;CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;} /*** 計(jì)算一行文字size*/ - (CGSize)sizeWithFont:(UIFont *)font {return [self sizeWithFont:font maxW:MAXFLOAT]; }

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/Cyan-zoey/p/5133167.html

    總結(jié)

    以上是生活随笔為你收集整理的iOS 之UITextFiled/UITextView小结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。