UITextView 中的控制选择

发布于 2024-12-19 13:00:27 字数 412 浏览 1 评论 0原文

我不希望用户能够选择我的 UITextView 的前几个字符。我尝试对其进行子类化并注意到诸如 -setSelectedRange:-setSelectedTextRange: 之类的方法。两者都在不同的时间被调用,但似乎我需要的是后者。

-setSelectedTextRange: 方法采用一个 UITextRange 对象,该对象具有一个名为“start”的 UITextPosition 属性。这听起来像是我想要的,但我无法写入它,并且该对象没有类。

有人对我如何做到这一点有任何想法吗? FWIW,我正在尝试复制 Facebook 在 iPhone 应用程序上的“签到”视图。

提前致谢!

I do not want the user to be able to select the first few characters of my UITextView. I have tried subclassing it and noticed methods such as -setSelectedRange: and -setSelectedTextRange:. Both are called at different times but it seems like it's the latter that I need.

The -setSelectedTextRange: method takes a UITextRange object, which has a UITextPosition property called "start". This sounds like what I want but I cannot write to it, and there are no classes for this object.

Does anyone have any ideas on how I can do this? FWIW, I'm trying to replicate what Facebook have on their "Check-In" view on their iPhone app.

Thanks in advance!

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

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

发布评论

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

评论(1

本宫微胖 2024-12-26 13:00:27

我个人并不熟悉 Facebook 应用程序签入视图的功能,但根据您的描述,听起来您的子类中需要类似的东西:

- (BOOL)becomeFirstResponder
{
    if ([super becomeFirstResponder]) {
        // Select text in field.        
        [self setSelectedTextRange:[self textRangeFromPosition:[self positionFromPosition:self.beginningOfDocument offset:1] toPosition:self.endOfDocument]];
        return YES;
    }
    return NO;
}

特别是,请注意“offset:1”参数。您应该能够使用它来设置所选文本范围的开头。此外,您还需要确保指定的新文本范围对于文本字段中的字符数有效。

I'm not personally familiar with the functionality of the Facebook app Check-In view, but based on your description, it sounds like you need something like this in your subclass:

- (BOOL)becomeFirstResponder
{
    if ([super becomeFirstResponder]) {
        // Select text in field.        
        [self setSelectedTextRange:[self textRangeFromPosition:[self positionFromPosition:self.beginningOfDocument offset:1] toPosition:self.endOfDocument]];
        return YES;
    }
    return NO;
}

In particular, note the "offset:1" argument. You should be able to use this to set the start of your selected text range. Also, you'll want to make sure that the new text range you specify is valid for the number of characters that are in the text field.

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