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

时间:2015-06-06 02:29:29

标签: ios objective-c iphone uitextfield uitextview

你怎么能创建一个UITextField非常类似苹果在他们的消息应用程序,Instagram使用,edmodo使用,Whatsapp使用和许多其他消息共享应用程序中使用的?此文本字段应设置动画,动画显示,可以展开,只能达到一定高度,并具有其他属性。

2 个答案:

答案 0 :(得分:5)

我研究了一段时间,但找不到答案所以我只是创造了自己的方式去做。你必须做几件事。首先,这确实使用了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

This is what the story board should look like and what it should look like when the application first opens. You will need to of course hook up all outlets

在.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";
    }
}
}

这就是一切。我希望这可以帮助那些尝试这样做的人,我希望你能将它整合到你的app中。 This is what the finish should look like.

答案 1 :(得分:1)

有一个可以用于增长文本视图的窗格: https://cocoapods.org/pods/HPGrowingTextView

通过这种方式,您可以管理动画和文本视图的最大高度。

相关问题