创建 UITextField/UITextView 类似于 iPhone 的消息传递应用程序

Create UITextField/UITextView similar to iPhone messaging app

如何创建一个 UITextField 非常类似于 apple 在其消息传递应用程序、Instagram 使用、edmodo 使用、Whatsapp 使用以及许多其他消息共享应用程序中使用的 UITextField?此文本字段应向上动画、向下动画、能够展开、仅达到一定高度,并且还具有其他属性。

我研究了一段时间,但找不到答案,所以我创建了自己的方法。有几件事你必须做。首先,这确实使用了 UITextView 而不是 UITextField。其次,此代码是专门为 iPhone 5 编写的,因此您可能需要调整坐标。它不使用大小 类 或约束。 1您需要先实施。您的 .h 文件应如下所示:

int returnPressed = 0;
int newLine;

@interface ViewController : UIViewController <UITextViewDelegate>
{
IBOutlet UIView *dock;
IBOutlet UIButton *sendButton;
IBOutlet UITextView *textView1;

CGRect previousRect;

}

@end

在 .m 文件中,需要满足许多要求才能使其看起来像 iPhone 消息应用程序的外观。你会注意到我们是如何在这里创建我们自己的自定义占位符的,因为 textview 并没有它们。这是 .m 文件:

- (void)viewDidLoad {
[super viewDidLoad];

previousRect = CGRectZero;

textView1.delegate = self;

self->textView1.layer.borderWidth = 1.0f;
self->textView1.layer.borderColor = [[UIColor lightGrayColor] CGColor];
self->textView1.layer.cornerRadius = 6;
self->textView1.textColor = [UIColor lightGrayColor];
self->textView1.text = @"Place Holder";






    }


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {

    returnPressed +=1;

    if(returnPressed < 17){

    textView1.frame = CGRectMake(8, 8, textView1.frame.size.width, textView1.frame.size.height + 17);

    newLine = 17*returnPressed;

    [UIView animateWithDuration:0.1 animations:^
     {
         dock.transform = CGAffineTransformMakeTranslation(0, -250 - newLine);
     }
     ];

    }
}

return YES;
}


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

UITextPosition* pos = textView1.endOfDocument;

CGRect currentRect = [textView1 caretRectForPosition:pos];

if (currentRect.origin.y > previousRect.origin.y || [textView1.text isEqualToString:@"\n"]){

    returnPressed +=1;

    if(returnPressed < 17 && returnPressed > 1){

        textView1.frame = CGRectMake(8, 8, textView1.frame.size.width, textView1.frame.size.height + 17);

        newLine = 17*returnPressed;

        [UIView animateWithDuration:0.1 animations:^
         {
             dock.transform = CGAffineTransformMakeTranslation(0, -250 - newLine);
         }
         ];

    }
}
previousRect = currentRect;


}



- (BOOL)textViewShouldBeginEditing:(UITextView *)textField {


if([textView1.text isEqualToString:@""] || [textView1.text isEqualToString:@"Place Holder"]){
textView1.text = @"";
}

textView1.textColor = [UIColor blackColor];

[UIView animateWithDuration:0.209 animations:^
 {
     dock.transform = CGAffineTransformMakeTranslation(0, -250 - newLine);
 }
                 completion:^(BOOL finished){}];

return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
    [textView1 resignFirstResponder];
    [self.view endEditing:YES];

    int height = returnPressed*20;

    [UIView animateWithDuration:0.209 animations:^
     {
         dock.transform = CGAffineTransformMakeTranslation(0, -height);
     }];

    if([textView1.text isEqualToString:@""]){
        self->textView1.textColor = [UIColor lightGrayColor];
        self->textView1.text = @"Place Holder";
    }
}
}

这就是一切。我希望这可以帮助任何试图这样做的人,我希望你可以将它集成到你的应用程序中。

有一个 pod 可用于增加文本视图: https://cocoapods.org/pods/HPGrowingTextView

通过它您可以管理动画和文本视图的最大高度。