- 追加された行はこの色です。
- 削除された行はこの色です。
&tag(TTTAttributedLabel);
*目次 [#l97680f2]
#contents
*参考情報 [#r5dcb6eb]
-[[mattt/TTTAttributedLabel · GitHub:https://github.com/mattt/TTTAttributedLabel]]
*インストール [#bb2b7239]
*概要 [#h91f2d19]
-部分的にリンクを作ったり、文字や色を変えたりすることの出来るラベルコントロール。
-ARCを使っている。
*ダウンロード [#bb2b7239]
-git cloneを使う
git clone https://github.com/mattt/TTTAttributedLabel.git
*サンプル [#f567488d]
-Example以下にiOS用のサンプルがある。
-リンクをクリックすればSafariで開くことができる。
-テーブルビューのサンプル。AttributedTableViewCellが内部で、TTTAttributedLabelを使用している。
-セル中のリンクをクリックすればSafariで開くことができる。
*使用方法 [#q6a9beb7]
-文字の装飾を行いたいときは setText:afterInheritingLabelAttributesAndConfiguringWithBlockを使う。
#pre{{
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor darkGrayColor];
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 0;
NSString *text = @"Lorem ipsum dolar sit amet";
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"ipsum dolar" options:NSCaseInsensitiveSearch];
NSRange strikeRange = [[mutableAttributedString string] rangeOfString:@"sit amet" options:NSCaseInsensitiveSearch];
// Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:14];
CTFontRef font = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)font range:boldRange];
[mutableAttributedString addAttribute:@"TTTStrikeOutAttribute" value:[NSNumber numberWithBool:YES] range:strikeRange];
CFRelease(font);
}
return mutableAttributedString;
}];
}}
-リンククリックを処理したい場合、addLinkToURLをつかう。
#pre{{
label.dataDetectorTypes = UIDataDetectorTypeAll; // Automatically detect links when the label text is subsequently changed
label.delegate = self; // Delegate methods are called when the user taps on a link (see `TTTAttributedLabelDelegate` protocol)
label.text = @"Fork me on GitHub! (http://github.com/mattt/TTTAttributedLabel/)"; // Repository URL will be automatically detected and linked
NSRange range = [label.text rangeOfString:@"me"];
[label addLinkToURL:[NSURL URLWithString:@"http://github.com/mattt/"] withRange:range]; // Embedd
}}
-任意の文字をリンクとして扱い、タップしたときの処理を行いたい場合action shcemeを使う([[iphone - UILabel - string as text and links - Stack Overflow:http://stackoverflow.com/questions/8839464/uilabel-string-as-text-and-links]])。
#pre{{
TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = @"Lost? Learn more.";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:@"Learn more"];
[tttLabel addLinkToURL:[NSURL URLWithString:@"action:show-help"] withRange:r];
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
if ([[url scheme] hasPrefix:@"action"]) {
if ([[url host] hasPrefix:@"show-help"]) {
/* load help screen */
} else if ([[url host] hasPrefix:@"show-settings"]) {
/* load settings screen */
}
} else {
/* deal with http links here */
}
}
}}
*自作アプリへの組み込み [#o9178a47]
-TTAttributedLabel.h/mを追加するだけ。
-non-ARCプロジェクトの場合、Build PhasesでTTAttributedLabel.mをクリックし"-fobjc-arc"を追加する。