Multi-line TextField (similar to SMS) and / or 'Done' button in UITextView

前端 未结 5 1144
臣服心动
臣服心动 2020-12-09 22:40

I\'ve been researching this for a few days now, and would appreciate a little help. Is there any way to generate a multi-line UITextField like Apple use in the SMS applicati

5条回答
  •  悲&欢浪女
    2020-12-09 23:27

    Well, I had a similar problem, and what I ended up using is actually create a disabled UITextField as the background and a UITextView above it to get the input... It sucks that iPhone API cannot have this by default. Also note that this does not auto-expand, but you can do this if you want by handling the textViewDidChange:

    As for handling the return key, try implementing the following method from the UITextViewDelegate:

    - (void)textViewDidChange:(UITextView *)inTextView {
        NSString *text = inTextView.text;
    
        if ([text length] > 0 && [text characterAtIndex:[text length] -1] == '\n') {
            inTextView.text = [text substringToIndex:[text length] -1]; // remove last return from text view
            [inTextView resignFirstResponder]; // hide keyboard
        }
    }
    

提交回复
热议问题