在UITextView中添加按钮和文本



嗨,朋友们,我想创建一个可以添加文本和按钮的视图。我想维护序列。就像在文本视图中一样,我想添加文本,然后添加按钮,然后再添加文本。同时也希望在同一区域进行编辑。我可以在UITextview中添加按钮,但如何维护我在按钮之后和任何按钮之前添加的文本序列?有它的示例代码吗?

我正在通过在UITextView中添加按钮

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton .frame = CGRectMake(100, myTextView.contentSize.height, 90, 30);
[myButton setTitle:@"button text" forState:UIControlStateNormal];
[myTextView addSubview:myButton ];

但是我不能在这个按钮之后移动光标。

是否可以不以相同的方式添加UILabel和UITextField控件,每次都使用contentSize.height?

为文本视图的父级创建一个uiview子类,并覆盖命中测试,类似于:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  // Check if the touch occurs within a rect or below a certain point
  BOOL isInsidePassThroughArea = point.y + self.textView.contentOffset.y < 72.f;
  if (isInsidePassThroughArea) {
    return [self.contentView hitTest:point withEvent:event];
  }
  return [super hitTest:point withEvent:event];
}

最新更新