【问题标题】:Text scrolls outside of the UITextView box boundary文本滚动到 UITextView 框边界之外
【发布时间】:2011-11-28 07:18:54
【问题描述】:

我有一个 UITextView,里面有一些文字。问题是文本滚动到 UITextView 框的边界之外。 (UITextView 不可编辑。)

这是代码以及我尝试解决此问题的方法:

  - (void)viewDidLoad {

        textBG.contentInset = UIEdgeInsetsZero;

    //  textBG.layer.masksToBounds = NO;
        textBG.layer.cornerRadius = 10.0;
        textBG.layer.borderWidth = 0.0;
        [textBG setClipsToBounds:YES];

    [super viewDidLoad];
}

- (void)textViewDidBeginEditing:(UITextView*)textView
{

textBG.contentInset = UIEdgeInsetsZero;    
 [textBG setClipsToBounds:YES];   
}

- (void) shouldChangeTextInRange:(UITextView*)textView {

    textBG.contentInset = UIEdgeInsetsZero;    
    [textBG setClipsToBounds:YES];

}

感谢您的帮助

【问题讨论】:

  • "text scrolls outside of the boundaries" 是什么意思?
  • 我添加了问题截图。

标签: iphone objective-c ios uitextview


【解决方案1】:

在 textViewDidBeginEditing 等 textView 委托方法中写入

textBG.contentInset = UIEdgeInsetsZero;

在 viewDidload 中

[textBG setClipsToBounds:YES];

【讨论】:

  • 我在两天前遇到了同样的问题,只是我把 textBG.contentInset = UIEdgeInsetsZero;在 shouldChangeTextInRange 和 textViewDidBeginEditing 方法中。在我的情况下它工作正常..
  • 然后把 [textBG setClipsToBounds:YES];我认为它会正常工作
  • 我试过你的答案,它适用于文本超出边界的问题。但是我的视图有阴影,当我使用clipsToBounds = YES 时,阴影不会出现。有什么办法可以保留阴影吗? @Narayana
  • @Gon:你找到解决方案了吗?
  • 为我工作。 textBG.textView.contentInset = UIEdgeInsets.zero in swift 4
【解决方案2】:

尝试在 XIB 中为您的 TextView 更改如下:

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,所以我追查了源头......这是因为在添加阴影时不知何故你扰乱了它的 ma​​sksToBounds 属性。因此,在添加更多文本时,它会溢出。我找到的简单解决方案是在添加阴影后编写这个简单的行。

    [self.myTextView setClipsToBounds:YES];
    

    通常应该在 viewDidLoad 方法中,但请记住仅在 应用 shadow 之后编写此行。

    感谢您阅读本文。

    【讨论】:

      【解决方案4】:

      只要放这一行代码就可以解决问题了。

      yourTextView.clipsToBounds = true
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多