0

トークン内の個々の文字を選択または削除できないように、のトークンとして使用したいNSArrayofがあります。全体として、バックスペースを押すと、個々の文字だけでなく関数全体が削除される電卓のような効果を作成したいと考えています。トークンは、他のテキストと一緒に強調表示できる必要もあります。NSStringUITextField

これを行うにはどうすればよいですか?

( Calc 2Mに見られるテキスト フィールドの動作を実現しようとしています)

編集:

テキストフィールドに属性付き文字列を使用させることでこれを解決し、トークンに特別な色を設定しました。次に、ジェスチャ レコグナイザーを使用してテキスト選択をインターセプトし、実際の選択ではなくトークンを囲むように選択の開始点と終了点を移動するイベントを発生させます。これが最善の方法だとは思わないので、回答ではなく編集として残します。

編集2:

私のものと一致する別の質問: <UITextinputDelegate> プロトコルの実装に関する問題

4

2 に答える 2

0

さて、これを回避するためにたくさんの方法を試した後、 asUITextViewの代わりに次の方法を使用することにしました。UITextFieldUITextFieldDelegate

- (void)textViewDidChangeSelection:(UITextView *)textView

でトークンを目立たせたかったのでUITextView、 を使用NSAttributedStringしてテキストを強調表示することにしました。トークンは通常のテキストとは異なる属性を持つようになったため、次を使用して、選択したテキストがトークンであるかどうかを確認できます。

- (id)attribute:(NSString *)attributeName atIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange

UITextPosition指定された aに対して any を受け取りUITextView、最も近いトークンの左側の開始点を a の形式で返すメソッドを作成しましたNSIntegerNSRangeこれは、後で簡単に使用できるようにするためです。

- (NSInteger)textViewWrappedIntegerFromPosition:(UITextPosition *)position forTextField:(UITextView *)textView
{
    NSInteger integer = [textView offsetFromPosition:textView.beginningOfDocument toPosition:position];
    NSInteger newInteger = integer;
    UIColor *color = [textView.attributedText attribute:NSForegroundColorAttributeName atIndex:integer-1 effectiveRange:NULL];

    // left of it is a character from a token
    if ([[UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0] isEqual:color])
    {
        NSInteger newInteger = integer+1;
        UITextPosition *startMinus = [textView positionFromPosition:position offset:-1];
        NSInteger startMinusInt = [textView offsetFromPosition:textView.beginningOfDocument toPosition:startMinus];
        UIColor *colorMinus = [textView.attributedText attribute:NSForegroundColorAttributeName atIndex:startMinusInt effectiveRange:NULL];

        // left and right of it is a character from a token
        if ([[UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0] isEqual:colorMinus]) // both collision
        {
            while ([colorMinus isEqual:[UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0]] && startMinusInt > 0)
            {
                // I used offset -1 again because when I wrote this I was testing something else
                position = [textView positionFromPosition:position offset:-1];
                startMinusInt = [textView offsetFromPosition:textView.beginningOfDocument toPosition:position];
                colorMinus = [textView.attributedText attribute:NSForegroundColorAttributeName atIndex:startMinusInt effectiveRange:NULL];
            }
            if (startMinusInt != 0)
            {
                newInteger = startMinusInt+1;
            }
            else
            {
                newInteger = startMinusInt;
            }
            return newInteger;
        }
        return newInteger;
    }
    return newInteger;
}

このメソッドを使用すると、選択範囲の and がトークン内またはトークンに隣接しているかどうかを計算できstartend UITextPosition新しい範囲の作成に進み、それを新しい選択範囲として設定できます。

[textView setSelectedRange:NSRangeMake(newStartInt,newEndInt-newStartInt];
于 2013-09-21T17:12:16.913 に答える
0

OS X では、これはNSTokenField. iOS での公式の実装はありませんが、数人が独自に開発しました。いくつかの例については、この質問を確認してください: NSTokenField コントロールに相当する iPhone はありますか?

于 2013-09-09T11:02:40.370 に答える