0

-目標: UITextView でテキストの特定のセクションのオリジンを取得する

以下は、私が取り組んでいるアプリのスクリーンショットへのリンクです。そちらの方が分かりやすいのでご覧ください。画像リンク:スクリーンショット

これは、穴埋めスタイルのゲームの始まりです。現在、各アンダースコアの x、y 座標を取得しようとしています。画面下部の単語をタップすると、次の使用可能なアンダースコア スペースに移動します。

現在、必要なことを行うためにこのコードを作成しましたが、非常に醜く、ほとんど機能せず、柔軟性もあまりありません。下記参照:

        // self.mainTextView is where the text with the underscores is coming from
        NSString *text = self.mainTextView.text;
        NSString *substring = [text substringToIndex:[text rangeOfString:@"__________"].location];



        CGSize size = [substring sizeWithFont:self.mainTextView.font];


        CGPoint p = CGPointMake((int)size.width % (int)self.mainTextView.frame.size.width, ((int)size.width / (int)self.mainTextView.frame.size.width) * size.height);




        // What is going on here is for some reason everytime there is a new
        // line my x coordinate is offset by what seems to be 10 pixels...
        // So was my ugly fix for it.. 
        // The UITextView width is 280

        CGRect mainTextFrame = [self.mainTextView frame];

        p.x = p.x + mainTextFrame.origin.x + 9;

        if ((int)size.width > 280) {
            NSLog(@"width: 280");
            p.x = p.x + mainTextFrame.origin.x + 10;
        }
        if ((int)size.width > 560) {
            NSLog(@"width: 560");
            p.x = p.x + mainTextFrame.origin.x + 12;

        }
        if ((int)size.width > 840) {
            p.x = p.x + mainTextFrame.origin.x + 14;

        }

        p.y = p.y + mainTextFrame.origin.y + 5;

        // Sender is the button that was pressed
        newFrame = [sender frame];
        newFrame.origin = p;

        [UIView animateWithDuration:0.2
                          delay:0
                        options:UIViewAnimationOptionAllowAnimatedContent|UIViewAnimationCurveEaseInOut
                     animations:^{

                         [sender setFrame:newFrame];
                     }
                     completion:^(BOOL finished){

                     }
         ];

ですから、私が尋ねる最善の質問は、これ を行うためのより良い方法は何ですか? または何か提案はありますか?これについてどう思いますか?

よろしくお願いいたします。

4

1 に答える 1