5

タイトルが示すように、UILabel の特定の部分に下線を引く必要があります。例: Please click ESPNSoccernet to read the latest Football News.ESPNSoccernet という単語に下線を引きたいと思います。これは、クリック可能にしたいためで、Web サイトにリンクする必要があります。

これを行うにはいくつかのガイダンスが必要です。他に方法があれば教えて...

4

7 に答える 7

15

iOS 6 では、AttributedStrings を使用できます

NSMutableAttributedString *yourString = [[NSMutableAttributedString alloc] initWithString:@"Please click ESPNSoccernet to read the latest Football News."];
[yourString addAttribute:NSUnderlineStyleAttributeName
                        value:[NSNumber numberWithInt:1]
                        range:(NSRange){0,25}];

label.attributedText = [yourString copy];

サードパーティの UILable ライブラリTTTAttributedLabelを使用することもできます。

于 2013-05-13T10:29:34.887 に答える
2

スウィフト 2.0:

1) 下線付きの nsmutablestring を作成し、sampleLabel のテキストに追加します。

2) タップ ジェスチャを sampleLabel に追加し、さらなるアクションのためのメソッドを関連付けます。

override func viewDidLoad() {
    super.viewDidLoad()

    let newsString: NSMutableAttributedString = NSMutableAttributedString(string: "Tap here to read the latest Football News.")
    newsString.addAttributes([NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleDouble.rawValue], range: NSMakeRange(4, 4))
    sampleLabel.attributedText = newsString.copy() as? NSAttributedString

    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
    tapGesture.numberOfTapsRequired = 1
    sampleLabel.userInteractionEnabled =  true
    sampleLabel.addGestureRecognizer(tapGesture)

}
func tapResponse(recognizer: UITapGestureRecognizer) {
    print("tap")
}
于 2016-02-11T09:55:10.180 に答える
2

Xcode の場合:

  1. ラベルを選択し、ID インスペクターを選択します。
  2. テキストでは、プレーンではなく属性を選択します。
  3. 下線を引くテキストの部分を選択します。
  4. フォントを選択し、フォント スタイルで下線を選択します。

ほらね。

于 2014-02-13T04:58:45.000 に答える
1

さて、私はこのような同じことをしました:

テキストでカスタム ボタンを作成します: ESPNSoccernet と背景を clearColor 高さ 1 のサブビューとしてラベルを追加し、このボタンに背景色を付けて、"ESPNSoccernet" に下線が引かれるようにします。残りのテキストは、このボタンに隣接するラベルに配置して、テキスト全体のように見せます。

それが役に立てば幸い!

注: >iOS 6.0 のみを実行している場合は、他の回答を確認することをお勧めします。

于 2013-05-13T10:40:22.207 に答える
1

UILabelはプレーンテキスト文字列を表示することしかできません(iOS 6 ではNSAttributedStrings も表示できるようになりましたが、これは古い iOS バージョンでは機能しないため、これに依存しないことが最善です)。ラベル。

属性付きのテキストを表示するためにTTTAttributedLabelを参照できますが (下線やその他の書式を追加できます)、このクラスでハイパーリンクを追加することはできません。

文字列のクリック可能なセグメントに対して使用できるオプションは、基本的に次のとおりです。

  1. クリック可能にしたい部分にプレーンUILabelとオーバーレイを使用する、またはUIButton

  2. を使用TTTAttributedLabelして下線効果を実現し、を使用してタップを検出して処理します (これは下線部分だけでなく、ラベル全体UITapGestureRecognizerのタップをキャプチャすることに注意してください)。

iOS 6 の場合:

UILabel *label = [[UILabel alloc] init];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Tap here to read the latest Football News."];
[string addAttribute:NSUnderlineStyleAttributeName value:@(1) range:NSMakeRange(4, 4)];
label.attributedText = [string copy];

以前の iOS バージョンおよび iOS 6 の場合:

TTTAttributedLabel *label = [[TTTAttributedLabel alloc] init];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"Tap here to read the latest Football News."];
[string addAttribute:NSUnderlineStyleAttributeName value:@(1) range:NSMakeRange(4, 4)];
label.text = [string copy];

次に、ジェスチャー認識エンジンを追加して実装しhandleTap:ます。

UITapGestureRecognizer *recogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[label addGestureRecognizer:recogniser];

- (void)handleTap:(UITapGestureRecognizer *)recogniser {
    // Handle the tap here
}
于 2013-05-13T10:30:27.127 に答える
0

その場合、ios 6以降のバージョン用のこのアプリの場合、NSMutableAttributedStringを使用できます

  NSMutableAttributedString *labelText = [[NSMutableAttributedString alloc] initWithString:@"Hello this is demmy label for testing"];
    [labelText addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:1]
       range:(NSRange){10,10}];

    label.attributedText = labelText;

より少ないバージョンでは、このように使用できます..

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 116, 171, 20)];
label.text = @"Hello this is demmy label for testing";
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:16];
[self.view addSubview:label];

//underline code
CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:tagcount] sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:UILineBreakModeWordWrap];

UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=CGRectMake((label.frame.size.width - expectedLabelSize.width)/2,    expectedLabelSize.height + (label.frame.size.height - expectedLabelSize.height)/2,   expectedLabelSize.width, 1);
viewUnderline.backgroundColor=[UIColor whiteColor];
[self.view addSubview:viewUnderline];
于 2013-05-13T10:40:01.357 に答える