2013-06-04 52 views
1

我在應用程序上創建,我需要輸入價格。客戶端需要自己的設計鍵盤,所以我開發下面的鍵盤。它工作完美。問題是當文字大於UITextField時,那麼它顯示點。我搜索谷歌和SO但沒有發現任何東西。
how to avoid dots next to a uitextfield
How to remove dots in UITextfield?和其他的答案,但在我的情況下不能正常工作。 當我用默認的鍵盤它滾動文本我輸入的號碼從UITextField刪除點

我的鍵盤是

enter image description here

時長度大於TextFied寬度則顯示什麼

enter image description here

我的代碼是

 - (IBAction)numberPressed:(id)sender { 
     UIButton *btn=(UIButton *)sender; 
     int number=btn.tag; 

     if (number <= 9) 
      txtPrice.text = [NSString stringWithFormat:@"%@%d",txtPrice.text, number]; 
     //decimal point 
     else if (number == 10) { 
      if ([txtPrice.text rangeOfString:@"."].location == NSNotFound) 
       txtPrice.text = [NSString stringWithFormat:@"%@.", txtPrice.text]; 
      } 
     //0 
     else if (number == 11) 
      txtPrice.text = [NSString stringWithFormat:@"%@0", txtPrice.text]; 
     //backspace 
     else if (number == 12) { 
      if ([txtPrice.text length] > 0) 
       txtPrice.text = [txtPrice.text substringToIndex:[txtPrice.text length] - 1]; 
      else 
       txtPrice.text = @""; 
     } 
    } 


    #pragma mark - 
    #pragma mark -TextField Delegate Method 

    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
     [self showKeyboard]; 
     return NO; // Hide both keyboard and blinking cursor. 
    } 

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
    { 
     return YES; 
    } 
+0

你使用'UITextField'還是'UILabel'?當UILabel變得太長時,它會自動截斷它的內容。 – Greg

+0

你想讓它顯示什麼? – kushyar

+0

@PartiallyFinite感謝您的重播,我用UITextField –

回答

0

如果你想有一個類似的行爲計算器或手機應用程序,你必須將以下屬性設置爲true(YES):

textField.adjustsFontSizeToFitWidth = YES; 

你也應該設置minimumFontSize屬性設置爲「防止接收器從縮小字體大小到不再清晰可見的地步。「

檢查出UITextField Reference

+0

我用UITextField不是UILabel –

+1

lineBreakMode不適用於UITextField – NiravPatel

+0

@NiravPatel謝謝,修正它。 – kushyar

1

UITextField是僅特異性一行。因此,無論UITextField在達到最終結果時都會顯示大點。

您需要使用UITextView而不是UITextField來顯示和編輯multiline文本。

在Interface Builder添加你想要它UITextView,然後選擇「編輯」複選框。默認情況下它將是multiline

我想這會幫助你。^_^

+0

不,不顯示點。但滾動字符.... –

+0

沒關係,這是避免顯示點的唯一方法。使用支持多行的UITextView。祝你好運!! – Atrash

+0

@MKAlatrash我不會說這是唯一的方法,但我可能會這樣做自己並處理多行,但如果用戶真的想堅持'UITextField',那麼他們將不得不去' kushyar'答案,但越早他們將再次處於相同的位置,因爲它將再次滿足minimumFontSize。 – Popeye

-4

你可以做的是

txtPrice.text = [txtPrice.text stringByReplacingOccurrencesOfString:@"." withString:@""]; 

讓我知道這是工作還是沒有!

快樂編碼!

+0

任何人都可以告訴我爲什麼這是錯誤的? – NiravPatel

+0

這將無法正常工作,因爲它正在逐詞包裝。所以當長度達到一定長度時,你會得到「3534 ...」,但是當你做'txtPrice.text'時,它會給出全文。 -1不正確的答案。 – Popeye

+0

整個議程是刪除點,我的代碼只是刪除點,而不是數字,所以我認爲這是正確的... – NiravPatel

1

你可以試試這個:

self.txtPrice.font = [UIFont systemFontOfSize: 14.0]; 
self.txtPrice.adjustsFontSizeToFitWidth = YES; 
self.txtPrice.minimumFontSize = 7.0; 

「adjustsFontSizeToFitWidth」是指示是否字體大小應以適合文本字符串轉換成文本字段的邊框可以減少一個布爾值。

「minimumFontSize」是最小允許字體與繪製文本字段的文本的大小。

0

另一種方法(刪除點和剪輯文本到框架) -

您可以刪除的UITextField調用點以下代碼

[self.yourTextField becomeFirstResponder];

,你也可以隱藏默認鍵盤[如果你使用任何自定義鍵盤]使用下面的代碼

//隱藏鍵盤撥號盤,但顯示閃爍的光標 的UIView * dummyKeyboardView = [[UIView的頁頭] initWithFrame:方法CGRectMake (0,0,1,1)]; yourTextField.inputView = dummyKeyboardView; [dummyKeyboardView發佈];

但我認爲IlNero的回答對你更好,如果你想顯示所有文字(不剪輯)。