关于页面有多个textfield点击换行,画面随之移动的方案

本文介绍了一种在iOS应用中实现当键盘弹出时自动滚动显示当前活动输入框的方法。通过监听键盘显示与隐藏通知调整UIScrollView的内容偏移,确保文本输入框始终可见。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果一个页面上有多个textfield,点击换行,让画面随之滚动,苹果给出了一个比较好的解决方案

Text, Web, and Editing Programming Guide for iOS


主要代码:

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(keyboardWasShown:)
            name:UIKeyboardDidShowNotification object:nil];
 
   [[NSNotificationCenter defaultCenter] addObserver:self
             selector:@selector(keyboardWillBeHidden:)
             name:UIKeyboardWillHideNotification object:nil];
 
}
 
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
 
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}
 
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}
 
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}


textfield自动换行可以通过设置TextField的属性来实现。根据引用[1]的代码,可以看到在文本内容中使用了一个for循环来判断每个字符的宽度是否超过了TextField的宽度减去5个像素。如果超过了,就在该字符的前一个位置插入换行符"\n"来实现换行。因此,根据这段代码可以得出一种实现自动换行的方式。 另外,根据引用中提供的解决方案,可以使用TextField的maxLines属性来限制文本的最大行数,当文本超过最大行数时会自动换行显示。 还有一种方式是使用LayoutBuilder来实现自动换行功能,参考引用中的代码。LayoutBuilder是一种布局工具,可以根据内容的长度和宽度来自动调整布局并实现自动换行的效果。这种方式需要使用额外的布局工具来实现,可以根据需要选择合适的方式来实现textfield的自动换行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [as3中textField输入字符时,一次性过长后自动换行](https://blog.youkuaiyun.com/weixin_30500473/article/details/95131095)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【Flutter 问题系列第 42 篇】TextField 如何根据内容自适应换行](https://blog.youkuaiyun.com/qq_42351033/article/details/119221619)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值