防止用户修改SLComposeViewController中的部分文本 [英] Prevent users from modifying part of the text in SLComposeViewController

查看:227
本文介绍了防止用户修改SLComposeViewController中的部分文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我们的代码设置为将Facebook / Twitter帖子的 UITextField 作为 setInitialText 获取文本。

Right now, our code is set to grab the text from the UITextField as setInitialText for Facebook/Twitter posts.

我们要做的是:向Facebook / Twitter帖子添加一个额外的永久消息或URL。

What we want to do is: add an additional permanent message or URL to the Facebook/Twitter posts.

我们该怎么做?这是我们当前的代码:

How can we do this? Here's our current code:

[slComposeThirdViewController setInitialText:[[self QText]text]];

[self presentViewController:slComposeThirdViewController animated:YES completion:nil];


推荐答案

有一点涉及,所以在这里忍受。 .. ,这只适用于SLServiceTypeTwitter

It's a little involved, so bear with me here... and this only works for SLServiceTypeTwitter

对于有兴趣使用这个的人,我已经在Github上放置了一个示例项目: https://github.com/NSPostWhenIdle/Immutable-SLComposeViewController

For anyone reading this that is interested in using this, I've put a sample project on Github: https://github.com/NSPostWhenIdle/Immutable-SLComposeViewController

您首先要做的是确保视图控制器符合 UITextViewDelegate 。您还需要为 UITextView 创建一个iVar。您实际上不会创建文本视图,但是您需要有一个指针直接分配给 SLComposeViewController 中的文本视图。当你在这里为永久字符串创建一个iVar。

The first thing you'll want to do is make sure that your view controller conforms to UITextViewDelegate. You'll also want to create an iVar for a UITextView. You won't actually be creating a text view, but you'll want to have a pointer to assign directly to the text view inside the SLComposeViewController. While you're here make a iVar for the permanent string as well.

@interface ViewController : UIViewController <UITextViewDelegate> //very important!
{
    UITextView *sharingTextView;
    NSString *permanentText;
}

然后在viewDidLoad中,您可以设置您想要的永久文本:

Then in viewDidLoad you can set up what you want the permanent text to be:

- (void)viewDidLoad
{
    [super viewDidLoad];
    permanentText = @"http://www.stackoverflow.com/";
}

下面的代码是一个非常基本的 IBAction 呈现作曲家的一些轻微的调整。首先,您会注意到, setInitialText 使用格式化的字符串,将永久文本添加到文本字段内容的末尾,并在其间添加一个空格。

The code below is a pretty basic IBAction to present the composer with a couple of slight tweaks. First, you'll notice that setInitialText uses a formatted string the append the permanent text to the end of the contents of the text field with a space added in between.

然后来重要的部分!我已经添加了一个循环到 presentViewController:的完成处理程序,以循环访问子视图子视图的子视图,以便识别 UITextView 包含共享文本的作曲家。这需要做,所以你可以设置文本视图的委托,以便访问 UITextViewDelegate 方法 shouldChangeTextInRange 。 p>

Then comes the important part! I've added a loop to presentViewController:'s completion handler to cycle through some subviews of subviews of subviews in order to identify the UITextView in the composer that contains the sharing text. This needs to be done so you can set that text view's delegate in order to access the UITextViewDelegate method shouldChangeTextInRange.

- (IBAction)exampleUsingFacebook:(UIButton *)sender {

    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *sharingComposer = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
            [sharingComposer dismissViewControllerAnimated:YES completion:nil];
        };
        [sharingComposer setCompletionHandler:completionHandler];
        [sharingComposer setInitialText:[NSString stringWithFormat:@"%@ %@",[[self QText]text],permanentText]];

        [self presentViewController:sharingComposer animated:YES completion:^{
            for (UIView *viewLayer1 in sharingComposer.view.subviews) {
                for (UIView *viewLayer2 in viewLayer1.subviews) {
                    if ([viewLayer2 isKindOfClass:[UIView class]]) {
                        for (UIView *viewLayer3 in viewLayer2.subviews) {
                            if ([viewLayer3 isKindOfClass:[UITextView class]]) {
                                [(UITextView *)viewLayer3 setDelegate:self];
                                sharingTextView = (UITextView *)viewLayer3;
                            }
                        }
                    }
                }
            }
        }];
    }
}

重要提示:请注意如果放在完成处理程序中,上面的只能工作。

Important: Please note that the above will only work if placed in the completion handler.

以下是如何设置 shouldChangeTextInRange 将用户尝试编辑的范围与包含永久文本的范围进行比较。通过这样做,用户将能够更改他们想要的文本的任何部分...除了包含您的永久文本的部分。你还会注意到,在这个方法之内,我将textView与shareingTextView进行了比较,这是我们在编辑器中分配给文本视图的指针。这样做将允许您在此控制器中使用其他文本视图,而不遵循我为作曲者中的文本视图配置的相同规则。

Below is an example of how to set up shouldChangeTextInRange to compare the range that the user is attempting to edit to the range that contains your permanent text. By doing so, the user will be able to make changes to any part of the text that they want... except for the part that contains your permanent text. You'll also notice that inside this method I've compared textView to shareingTextView, the pointer we assigned to the text view inside the composer. Doing so will allow you to use other text views within this controller without them following the same rules I've configured for the text view inside the composer.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (textView == sharingTextView) {
        NSRange substringRange = [textView.text rangeOfString:permanentText];
        if (range.location >= substringRange.location && range.location <= substringRange.location + substringRange.length) {
            return NO;
        }
    }
    return YES;
}

希望这有帮助!

这篇关于防止用户修改SLComposeViewController中的部分文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆