簡體   English   中英

如何更改 UITextView 和 UITextField 中的文本

[英]How to change text in UITextView and UITextField

我需要編輯我的UITextView ,當我進行這些更改時,我需要它們在我的UITextFields反映出來。 目前我可以使用下面的代碼將我寫的任何文本移動到第一個UITextField是否有一種簡單的方法可以將每一行移動到單獨的UITextField

像圖像一樣,除了 1、2 和 3 轉到不同的UITextField

-(IBAction)print:(id)sender {

NSString *texto = [[NSString alloc] initWithFormat:@"%@ %@",[myTextField text],[myTextView text]];
[myTextField setText:texto];
[myTextView setText:@""];
[myTextView resignFirstResponder];

}

像這樣

真的,我需要弄清楚這一點。 任何幫助將不勝感激。

讓您的視圖控制器符合UITextViewDelegate協議。

接下來,將文本視圖的delegate屬性設置為視圖控制器。

然后,按照以下方式實現- (void)textViewDidChange:(UITextView *)textView

- (void)textViewDidChange:(UITextView *)textView
{
    // Set the text for your text fields.
    //
    // (I'm assuming your code for determining the text to put in the
    // text fields works as you intend, so I'm just copying it here.)

    NSLayoutManager *layoutManager = [textView layoutManager];
    unsigned numberOfLines, index, numberOfGlyphs = [layoutManager numberOfGlyphs];
    NSRange lineRange;

    for (numberOfLines = 0, index = 0; index < numberOfGlyphs; numberOfLines++)
    {
        (void) [layoutManager lineFragmentRectForGlyphAtIndex:index
                                               effectiveRange:&lineRange];

        index = NSMaxRange(lineRange);
        NSString *lineText = [textView.text substringWithRange:lineRange];
        [yourArray addObject:lineText];
    }

    textField1.text=yourArray[0];
    textField2.text=yourArray[1];
    textField3.text=yourArray[2];
    textField4.text=yourArray[3];
    textField5.text=yourArray[4];
    textField6.text=yourArray[5];
    textField7.text=yourArray[6]; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM