如何阻止 UITextView 的底部边缘插入重置为 32?

发布于 2024-11-09 09:37:01 字数 657 浏览 0 评论 0原文

我有一个全屏 UITextView,每当键盘出现时它就会变小​​,这样键盘就不会覆盖任何文本。作为其中的一部分,我还更改了 textView 的底部 contentInset,因此当存在键盘时文本下方的空间较小,而当没有键盘时文本下方的空间较大。

问题是,每当用户点击底部附近的 textView 开始编辑时,底部 contentInset 会自发地将其重置为 32。我从 这个答案可以子类化 UITextView并覆盖 contentInset 方法,如下所示:

@interface BCZeroEdgeTextView : UITextView
@end

@implementation BCZeroEdgeTextView

- (UIEdgeInsets) contentInset 
  { 
  return UIEdgeInsetsZero; 
  }

@end

但这并不能阻止底部插图自行重置 - 它只是更改其重置为的数字。如何使我的 UITextView 保持我设置的 contentInset 值?

I have a full-screen UITextView that gets smaller whenever the keyboard appears, so that the keyboard doesn't cover any of the text. As part of this, I also change the textView's bottom contentInset, so the space below the text is smaller when the keyboard is present, and larger when there is no keyboard.

The problem is, whenever the user taps the textView near the bottom to start editing, the bottom contentInset spontaneously resets itself to 32. I know from this answer that it is possible to subclass the UITextView and override the contentInset method, like this:

@interface BCZeroEdgeTextView : UITextView
@end

@implementation BCZeroEdgeTextView

- (UIEdgeInsets) contentInset 
  { 
  return UIEdgeInsetsZero; 
  }

@end

But this doesn't stop the bottom inset resetting itself - it just changes the figure it resets itself to. How can I make my UITextView just keep to the contentInset value that I have set?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅倚清风 2024-11-16 09:37:01

为了使其保持您设置的值,您可以走子类路线,但返回您自己的属性值而不是常量,如下所示:

@interface BCCustomEdgeTextView : UITextView
@property (nonatomic, assign) UIEdgeInsets myContentInset;
@end

@implementation BCCustomEdgeTextView

@synthesize myContentInset;

- (UIEdgeInsets) contentInset { 
    return self.myContentInset; 
}

@end

但请注意, UITextView 将其底部 contentInset 重置为 32 的原因是更标准inset 将切断自动完成弹出窗口等。

To make it keep the value you set, you could go the subclass route but return the value of your own property rather than a constant, something like this:

@interface BCCustomEdgeTextView : UITextView
@property (nonatomic, assign) UIEdgeInsets myContentInset;
@end

@implementation BCCustomEdgeTextView

@synthesize myContentInset;

- (UIEdgeInsets) contentInset { 
    return self.myContentInset; 
}

@end

But do note that the reason UITextView resets its bottom contentInset to 32 is that a more standard inset will cut off the autocompletion popups and such.

忆悲凉 2024-11-16 09:37:01

这是我的解决方案,但有点长:

- (void)setCustomInsets:(UIEdgeInsets)theInset
{
    customInsets = theInset;
    self.contentInset = [super contentInset];
    self.scrollIndicatorInsets = [super scrollIndicatorInsets];
}

- (void)setContentInset:(UIEdgeInsets)theInset
{
    [super setContentInset:UIEdgeInsetsMake(
        theInset.top + self.customInsets.top,
        theInset.left + self.customInsets.left, 
        theInset.bottom + self.customInsets.bottom, 
        theInset.right + self.customInsets.right)];
}

- (void)setScrollIndicatorInsets:(UIEdgeInsets)theInset
{
    [super setScrollIndicatorInsets:UIEdgeInsetsMake(
        theInset.top + self.customInsets.top, 
        theInset.left + self.customInsets.left, 
        theInset.bottom + self.customInsets.bottom, 
        theInset.right + self.customInsets.right)];
}

子类 UITextView 并添加属性 customInsets,每当您需要设置 contentInsetscrollIndicatorInsets 时,设置 customInsets 来代替。

Here is my solution, but a bit longer:

- (void)setCustomInsets:(UIEdgeInsets)theInset
{
    customInsets = theInset;
    self.contentInset = [super contentInset];
    self.scrollIndicatorInsets = [super scrollIndicatorInsets];
}

- (void)setContentInset:(UIEdgeInsets)theInset
{
    [super setContentInset:UIEdgeInsetsMake(
        theInset.top + self.customInsets.top,
        theInset.left + self.customInsets.left, 
        theInset.bottom + self.customInsets.bottom, 
        theInset.right + self.customInsets.right)];
}

- (void)setScrollIndicatorInsets:(UIEdgeInsets)theInset
{
    [super setScrollIndicatorInsets:UIEdgeInsetsMake(
        theInset.top + self.customInsets.top, 
        theInset.left + self.customInsets.left, 
        theInset.bottom + self.customInsets.bottom, 
        theInset.right + self.customInsets.right)];
}

Subclass UITextView and add a property customInsets, whenever you need to set contentInset and scrollIndicatorInsets, set customInsets instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文