タイトルが示すように、UILabel の特定の部分に下線を引く必要があります。例: Please click ESPNSoccernet to read the latest Football News.
ESPNSoccernet という単語に下線を引きたいと思います。これは、クリック可能にしたいためで、Web サイトにリンクする必要があります。
これを行うにはいくつかのガイダンスが必要です。他に方法があれば教えて...
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を使用することもできます。
スウィフト 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")
}
Xcode の場合:
ほらね。
さて、私はこのような同じことをしました:
テキストでカスタム ボタンを作成します: ESPNSoccernet と背景を clearColor 高さ 1 のサブビューとしてラベルを追加し、このボタンに背景色を付けて、"ESPNSoccernet" に下線が引かれるようにします。残りのテキストは、このボタンに隣接するラベルに配置して、テキスト全体のように見せます。
それが役に立てば幸い!
注: >iOS 6.0 のみを実行している場合は、他の回答を確認することをお勧めします。
UILabel
はプレーンテキスト文字列を表示することしかできません(iOS 6 ではNSAttributedString
s も表示できるようになりましたが、これは古い iOS バージョンでは機能しないため、これに依存しないことが最善です)。ラベル。
属性付きのテキストを表示するためにTTTAttributedLabelを参照できますが (下線やその他の書式を追加できます)、このクラスでハイパーリンクを追加することはできません。
文字列のクリック可能なセグメントに対して使用できるオプションは、基本的に次のとおりです。
クリック可能にしたい部分にプレーンUILabel
とオーバーレイを使用する、またはUIButton
を使用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
}
その場合、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];