5

次のコードを使用して、UITextView の一部にタップ ジェスチャを正常に追加できました。

UITextPosition *pos = textView.endOfDocument;// textView ~ UITextView

for (int i=0;i<words*2-1;i++){// *2 since UITextGranularityWord considers a whitespace to be a word

    UITextPosition *pos2 = [textView.tokenizer positionFromPosition:pos toBoundary:UITextGranularityWord inDirection:UITextLayoutDirectionLeft];
    UITextRange *range = [textView textRangeFromPosition:pos toPosition:pos2];
    CGRect resultFrame = [textView firstRectForRange:(UITextRange *)range ];

    UIView* tapViewOnText = [[UIView alloc] initWithFrame:resultFrame];
    [tapViewOnText addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(targetRoutine)]];
    tapViewOnText.tag = 125;
    [textView addSubview:tapViewOnText];

    pos=pos2;
}

で同じ動作を模倣したいと思いUILabelます。問題は、UITextInputTokenizer(個々の単語をトークン化するために使用される) が で宣言され、 &UITextInput.hのみに準拠することです。ではない。これに対する回避策はありますか??UITextViewUITextFieldUITextInput.hUILabel

4

5 に答える 5

2

One option would be to use a non-editable UITextView instead of a UILabel. Of course this may or may not be a suitable solution depending on your exact needs.

于 2013-07-24T07:00:44.787 に答える
0

https://github.com/mattt/TTTAttributedLabelを試して、ラベルへのリンクを追加できます。リンクが押されるとアクションが発生するため、ラベルのクリックの一部は、ラベルのリンク部分をカスタマイズする必要がある場合にのみ機能します。過去にこれを試してみましたが、問題なく動作しましたが、クライアントはサードパーティ コンポーネントの使用に関心がなかったため、UIWebView と HTML を使用してこの機能を複製しました。

于 2013-07-24T07:46:37.860 に答える