我有一個UITextView,其中必須以編程方式編寫多行。當我這樣做:在文本視圖中寫入多行的問題
[textView setText:@"String number 1\n"];
[textView setText:@"String number 2\n"];
我只得到:String number 2
,彷彿這是寫在其他線路。我明白任何建議:)
我有一個UITextView,其中必須以編程方式編寫多行。當我這樣做:在文本視圖中寫入多行的問題
[textView setText:@"String number 1\n"];
[textView setText:@"String number 2\n"];
我只得到:String number 2
,彷彿這是寫在其他線路。我明白任何建議:)
顧名思義,setText:
是二傳手:它取代現有文本。
這是相當喜歡,如果你有一個int val
和你在做:
val = 5;
val = 8;
當然在年底VAL將等於8和值5將會丟失。這裏同樣的事情。
相反,你需要創建一個包含所有行的字符串,並直接影響到的TextView:
[textView setText:@"Line1\nLine2\nLine3\n"];
如果需要逐步做到這一點,說你需要添加文字在TextView的現有文本,首先檢索現有的文本,追加新的生產線,並設置新的背課文:
// get the existing text in the textView, e.g. "Line1\n", that you had set before
NSString* currentText = [textView text];
// append the new text to it
NSString* newText = [currentText stringByAppendingString:@"New Line\n"];
// affect the new text (combining the existing text + the added line) back to the textView
[textView setText:newText];
的setText取代的TextView的全部內容。您最好準備結果字符串,並通過單個setText方法調用進行設置。 例如:
NSString *resultString = [NSString stringWithFormat:@"%@%@", @"String 1\n", @"String 2\n"];
[textView setText:resultString];
根據How to create a multiline UITextfield?,UITextField
只有單線。相反,請使用UITextView
。
另外,正如其他人指出的那樣,如果您再次撥打setText
,則會覆蓋以前的值。相反,你應該做這樣的事情:
[textView setText:@"String number 1\nString number 2"]
可以追加字符串:
mTextview.text = [mTextview.text stringByAppendingString:aStr];
據的問題,他已經在使用一個TextView,而不是一個文本字段? – AliSoftware
Doh!完全設法誤解了這一點。 –