Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在聊天中创建不断增长和收缩的UITextView?_Ios_Objective C_Uitableview_Uitextview - Fatal编程技术网

Ios 如何在聊天中创建不断增长和收缩的UITextView?

Ios 如何在聊天中创建不断增长和收缩的UITextView?,ios,objective-c,uitableview,uitextview,Ios,Objective C,Uitableview,Uitextview,我尝试创建一个不断增长和收缩的UITextView,我尝试了一些示例代码,我可以看到它在增长,但没有收缩 当我输入文本时,它会变大,但当我从UITextView中删除相同的文本时,它不会缩小到原来的大小。 这是增长UITextView的工作代码 int returnPressed = 0; int newLine; @interface dynamicViewController () <UITextViewDelegate> { IBOutlet UIView *doc

我尝试创建一个不断增长和收缩的UITextView,我尝试了一些示例代码,我可以看到它在增长,但没有收缩

当我输入文本时,它会变大,但当我从UITextView中删除相同的文本时,它不会缩小到原来的大小。 这是增长UITextView的工作代码

int returnPressed = 0;
int newLine;

@interface dynamicViewController () <UITextViewDelegate>
{

    IBOutlet UIView *dock;
    IBOutlet UITextView *textView1;
    CGRect previousRect;

}






- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier;
   // NSLog(@"%@",selectedTabFields);
    if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){
        MyIdentifier = @"CharCell";

    }
    cell= [tableView1 dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[dynamicCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:MyIdentifier];
           cell.selectionStyle=UITableViewCellSelectionStyleNone;


            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.accessoryType = UITableViewCellAccessoryNone;

                [cell.contentView addSubview:[self getLabel:indexPath.row]];

            if([[[selectedTabFields valueForKey:@"type"] objectAtIndex:indexPath.row] isEqualToString:@"VARCHAR"]){

                [cell.contentView addSubview:[self getVarcharTextView:indexPath.row]];
            }

    }


    return cell;

}


//Varchar UITextView
-(UITextView *)getVarcharTextView:(NSUInteger)index{
    UITextField *textField;
    if(IS_IPHONE_5){
        textView1= [[UITextView alloc] initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
    }else if(IS_IPHONE_6){
        textView1= [[UITextView alloc] initWithFrame:CGRectMake(15,40,340,cell.frame.size.height)];

    }
    previousRect = CGRectZero;

    textView1.delegate = self;

    textView1.layer.borderWidth = 0.0f;
    textView1.layer.borderColor = [[UIColor clearColor] CGColor];
    textView1.layer.cornerRadius = 4;
    textView1.backgroundColor=[UIColor colorWithRed:249.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1];
    textView1.textColor = [UIColor lightGrayColor];
    textView1.text = @"Place Holder";
    return textView1;

}

- (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, -200 - newLine);
     }
                     completion:^(BOOL finished){}];

    return YES;
}
int returnPressed=0;
国际新线;
@接口dynamicViewController()
{
IBUIVIEW*船坞;
IBOUTLE UITextView*textView1;
CGRect-previousRect;
}
-(UITableViewCell*)tableView:(UITableView*)tableView1 cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*MyIdentifier;
//NSLog(@“%@”,所选数据库字段);
如果([[[selectedTabFields valueForKey:@“type”]objectAtIndex:indexPath.row]IsequalString:@“VARCHAR”]){
MyIdentifier=@“CharCell”;
}
cell=[tableView1 dequeueReusableCellWithIdentifier:MyIdentifier];
如果(单元格==nil)
{
单元格=[[dynamicCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.accessoryType=UITableViewCellAccessoryNone;
[cell.contentView addSubview:[self-getLabel:indexath.row]];
如果([[[selectedTabFields valueForKey:@“type”]objectAtIndex:indexPath.row]IsequalString:@“VARCHAR”]){
[cell.contentView addSubview:[self-getVarcharTextView:indexPath.row]];
}
}
返回单元;
}
//Varchar UITextView
-(UITextView*)getVarcharTextView:(整数)索引{
UITextField*textField;
如果(是IPHONE 5){
textView1=[[UITextView alloc]initWithFrame:CGRectMake(15,40,cell.frame.size.width-30,cell.frame.size.height)];
}否则如果(是IPHONE 6){
textView1=[[UITextView alloc]initWithFrame:CGRectMake(15,40340,cell.frame.size.height)];
}
previousRect=CGRectZero;
textView1.delegate=self;
textView1.layer.borderWidth=0.0f;
textView1.layer.borderColor=[[UIColor clearColor]CGColor];
textView1.layer.cornerRadius=4;
textView1.backgroundColor=[UIColor颜色带红色:249.0/255.0绿色:249.0/255.0蓝色:249.0/255.0 alpha:1];
textView1.textColor=[UIColor lightGrayColor];
textView1.text=@“占位符”;
返回textView1;
}
-(BOOL)文本视图:(UITextView*)文本视图应更改文本范围:(NSRange)范围替换文本:(NSString*)文本{
如果([文本IsequalString:@“\n”]){
返回值+=1;
如果(按下返回键<17){
textView1.frame=CGRectMake(8,8,textView1.frame.size.width,textView1.frame.size.height+17);
换行符=17*returnPressed;
[UIView animateWithDuration:0.1动画:^
{
dock.transform=CGAffineTransformMakeTransform(0,-250-换行符);
}
];
}
}
返回YES;
}
-(无效)textViewDidChange:(UITextView*)textView{
UITextPosition*pos=textView1.endOfDocument;
CGRect currentRect=[textView1 caretRectForPosition:pos];
if(currentRect.origin.y>previousRect.origin.y | |[textView1.text IsequalString:@“\n”])){
返回值+=1;
如果(返回按下<17和返回按下>1){
textView1.frame=CGRectMake(8,8,textView1.frame.size.width,textView1.frame.size.height+17);
换行符=17*returnPressed;
[UIView animateWithDuration:0.1动画:^
{
dock.transform=CGAffineTransformMakeTransform(0,-250-换行符);
}
];
}
}
previousRect=当前Rect;
}
-(BOOL)文本视图应开始编辑:(UITextView*)文本字段{
if([textView1.text IseQualtString:@”“]| |[textView1.text IseQualtString:@“placeholder”])){
textView1.text=@;
}
textView1.textColor=[UIColor blackColor];
[UIView animateWithDuration:0.209动画:^
{
dock.transform=CGAffineTransformMakeTransform(0,-200-换行符);
}
完成:^(BOOL finished){}];
返回YES;
}

请帮助使其完美。

我现在正在开发一个聊天应用程序,这就是我正在做的

下载此库

将所需文件拖放到项目中

使用cocoapods下载

现在拖放一个UIView而不是UITextView,并在interface builder中将UIView的自定义类设置为“HPGrowingTextView

现在,第一个库是针对iOS7及以下版本的,因此他已经编写了好几行代码。我所做的是,删除了一些代码。检查下面我的代码

@interface ChatMessagesViewController ()<HPGrowingTextViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UIView *containerView;
    IBOutlet HPGrowingTextView *msgTextView;
}
将此代码放入AppDelegate以删除特定ViewController中IQKeyboardManager的工具栏。这里我的ViewController是ChatMessageViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    [[IQKeyboardManager sharedManager] disableToolbarInViewControllerClass:[ChatMessagesViewController class]];

    return YES;
}
现在,您可以看到您的文本视图可以在键入时增加,也可以缩小。

注意-如果您不想使用IQKeyboardManager,则必须将observer添加为NSNotificationCenter以显示和隐藏键盘。像这样


这两种方法都清楚地写在库中,但我们也必须使用autolayout进行管理。因此,最好使用IQKeyboarManager,而且操作简单。

您计算按下“return”多少次的逻辑存在严重缺陷。您不处理用户删除任何文本,也不处理用户粘贴(可能替换)可能包含任何新行数的文本。您需要重新考虑
shouldChangeTextInRange
的实现,以应对所有这些可能性。它也不处理简单的单词包装。我清楚地提到,我从示例中得到了它。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    [[IQKeyboardManager sharedManager] disableToolbarInViewControllerClass:[ChatMessagesViewController class]];

    return YES;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}