Rya*_*bin 2 iphone objective-c uitextfield uitextview ios
你怎么能创建一个UITextField非常类似苹果在他们的消息应用程序,Instagram使用,edmodo使用,Whatsapp使用和许多其他消息共享应用程序中使用的?此文本字段应设置动画,动画向下,能够展开,仅达到某个高度,并具有其他属性.
我研究了一段时间,但找不到答案所以我只是创造了自己的方式来做到这一点.你必须做几件事.首先,这确实使用了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
Run Code Online (Sandbox Code Playgroud)

在.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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是一切.我希望这可以帮助那些尝试这样做的人,我希望你能将它整合到你的app中.

| 归档时间: |
|
| 查看次数: |
4529 次 |
| 最近记录: |