【发布时间】:2015-06-06 02:29:29
【问题描述】:
您如何创建一个与苹果在其消息应用程序中使用的一个非常相似的 UITextField,Instagram 使用,edmodo 使用,Whatsapp 使用以及许多其他消息共享应用程序?此文本字段应该向上动画、向下动画、能够扩展、只能达到一定高度,并且还具有其他属性。
【问题讨论】:
标签: ios objective-c iphone uitextfield uitextview
您如何创建一个与苹果在其消息应用程序中使用的一个非常相似的 UITextField,Instagram 使用,edmodo 使用,Whatsapp 使用以及许多其他消息共享应用程序?此文本字段应该向上动画、向下动画、能够扩展、只能达到一定高度,并且还具有其他属性。
【问题讨论】:
标签: ios objective-c iphone uitextfield uitextview
我研究了一段时间,但找不到答案,所以我只是创建了自己的方法来做到这一点。您必须做几件事。首先,这确实使用了 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
你可以管理动画和文本视图的最大高度。
【讨论】: