自动根据键盘位置调整UITextView的高度

这个应该是一个通用的任务了吧,键盘弹出来的时候,UITextView(或者UITextField)会被遮挡。 
解决的办法就不是很能通用了。 
1. 如果有UIScrollView做父view的话只需要滚动到合适的位置即可。 
2. 如果没有UIScrollView的话,可以恰当的临时调整一下UITextView的高度,使得最下面一行的输入也能被看到。 

下面只对第二种情况说明一下要点: 
我的做法是创建一个UITextView的派生类,这样可以方便重用。 
(不派生类也是可以的,原理一样。) 
注册2个Notification消息,分别是UIKeyboardDidShowNotification和UIKeyboardWillHideNotification 

表示键盘已经弹出来和键盘要消失的时候发送。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- ( void )registerForKeyboardNotifications {
     [[NSNotificationCenterdefaultCenter] addObserver:self
                                              selector:@selector(keyboardWasShow:)
                                                  name:UIKeyboardDidShowNotification
                                                object:nil];
     [[NSNotificationCenterdefaultCenter] addObserver:self
                                              selector:@selector(keyboardWillBeHidden:)
                                                  name:UIKeyboardWillHideNotification
                                                object:nil];
}
 
- ( void )unregisterForKeyboardNotifications{
     [[NSNotificationCenterdefaultCenter] removeObserver:self
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
     [[NSNotificationCenterdefaultCenter] removeObserver:self
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
}
不过,要注意的是这两个消息不是严格一一对应的。 

消息处理:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- ( void )keyboardWasShow:(NSNotification *)notification {
     // 取得键盘的frame,注意,因为键盘在window的层面弹出来的,所以它的frame坐标也是对应window窗口的。
     CGRect endRect = [[notification.userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
     CGPoint endOrigin = endRect.origin;
     // 把键盘的frame坐标系转换到与UITextView一致的父view上来。
     if ([UIApplicationsharedApplication].keyWindow && self.superview) {
         endOrigin = [self.superview convertPoint:endRect.originfromView:[UIApplicationsharedApplication].keyWindow];
     }
 
     CGFloat adjustHeight = originalContentViewFrame.origin.y + originalContentViewFrame.size.height;
     // 根据相对位置调整一下大小,自己画图比划一下就知道为啥要这样计算。
     // 当然用其他的调整方式也是可以的,比如取UITextView的orgin,origin到键盘origin之间的高度作为UITextView的高度也是可以的。
     adjustHeight -= endOrigin.y;
     if (adjustHeight > 0) {
 
         CGRect newRect = originalContentViewFrame;
         newRect.size.height -= adjustHeight;
         [UIViewbeginAnimations:nilcontext:nil];
         self.frame = newRect;
         [UIViewcommitAnimations];
     }
}
 
- ( void )keyboardWillBeHidden:(NSNotification *)notification{
     // 恢复原理的大小
     [UIViewbeginAnimations:nilcontext:nil];
     self.frame = originalContentViewFrame;
     [UIViewcommitAnimations];
}

BTW:

现在的云计算貌似已经很火热了,但是分享的渠道还是不能畅通,没有一个合适的分享协议。EverNote写好的笔记想要给人分享,就只有分享到twitter和facebook2个选择。

大家是怎么把笔记分享到博客平台的呢?

oschina有没有开放什么对外的接口,实在不行自己动手给EverNote做个分享的插件吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,您需要将 YYLabel 添加到 UITableViewCell 中。 您可以通过在 UITableViewCell 子类中创建 YYLabel 实例并将其添加到 contentView 中来完成此操作。 您可以使用自动布局或手动布局将 YYLabel 放置在所需的位置上。 接下来,您需要创建 NSMutableAttributedString,其中包含 UITextView 和其他文本。 然后,您可以使用 NSMutableAttributedString 的 insert 方法将 UITextView 作为附件添加到文本中。 以下是一个示例代码片段: ```swift let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 50)) textView.text = "This is a UITextView" let attachment = NSTextAttachment() attachment.bounds = CGRect(x: 0, y: 0, width: 100, height: 50) attachment.setAttachmentContent(textView) let attributedString = NSMutableAttributedString(string: "This is a YYLabel with a UITextView attachment") attributedString.insert(NSAttributedString(attachment: attachment), at: 21) yyLabel.attributedText = attributedString ``` 最后,您需要处理 UITextView 和 UITableView 之间的交互。您需要将 UITextView 的 isEditable 属性设置为 false,以防止用户编辑文本。您还需要在 UITableViewDelegate 中实现 heightForRowAt 和 estimatedHeightForRowAt 方法来动态计算 UITableViewCell 的高度,以适应包含 UITextView 的 YYLabel。 以下是一个示例 UITableViewDelegate 实现: ```swift class MyTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cell") } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell // Configure the cell... return cell } override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MyTableViewCell cell.configure(with: "This is a YYLabel with a UITextView attachment") return cell.systemLayoutSizeFitting(CGSize(width: tableView.frame.width, height: UIView.layoutFittingCompressedSize.height)).height } override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } } ``` 请注意,此示例实现了一个名为 MyTableViewCell 的 UITableViewCell 子类,该子类包含一个名为 yyLabel 的 YYLabel 实例。 该子类还实现了一个名为 configure(with:) 的方法,该方法接受一个字符串参数,并在 YYLabel 中设置带有 UITextView 附件的 NSMutableAttributedString。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值