TTTAttributedLabel
特徴
OHAttributedLabelと同じく,
OHAttributedLabelの後発なので,
OHAttributedLabelとTTTAttributedLabelのAPIの違いを比較しやすいよう,
使用準備
ソースコードを以下のURLよりダウンロード
CoreText.
frameworkをプロジェクトに追加 TTTAttributedLabel.
hをインポート #import "TTTAttributedLabel.
h"
使い方
OHAttributedLabelと使用方法の比較ができるよう,
赤字・
属性の指定にはaddAttribute:value:range:メソッドを用いるのですが,
[self.tttLabel setText:txt afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
CTFontRef baseFontRef = CTFontCreateWithName((CFStringRef)baseFont.fontName, baseFont.pointSize, NULL);
if (baseFontRef) {
NSRange allRange = NSMakeRange(0, [txt length]);
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName
value:(id)baseFontRef
range:allRange];
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)[[UIColor grayColor] CGColor]
range:allRange];
CFRelease(baseFontRef);
}
UIFont *boldFont = [UIFont boldSystemFontOfSize:18];
CTFontRef boldFontRef = CTFontCreateWithName((CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
if (boldFontRef) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName
value:(id)boldFontRef
range:strongRange];
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:(id)[strongColor CGColor]
range:strongRange];
CFRelease(boldFontRef);
}
return mutableAttributedString;
}];
リンクの指定はaddLinkToURL:withRange:メソッドをコールします。
[self.tttLabel addLinkToURL:linkUrl withRange:linkRange];
このメソッドだけ見るとOHAttributedLabelとほぼ同じなのですが,
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
[[UIApplication sharedApplication] openURL:linkUrl];
}
OHAttributedLabelはここはラップしてくれているのでひと手間増えることになりますが,
FxLabel
FxLabelは,
FxLabelを用いると,
使用準備
ソースコードを以下のURLよりダウンロード
FXLabel.
hをインポート #import "FXLabel.
h"
使い方
文字列に影を付けたい場合は,
label1.shadowOffset = CGSizeMake(0.0f, 2.0f);
label1.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.75f];
label1.shadowBlur = 5.0f;
サンプルにあるエンボス効果は,
label2.shadowColor = [UIColor colorWithWhite:1.0f alpha:0.8f];
label2.shadowOffset = CGSizeMake(1.0f, 2.0f);
label2.shadowBlur = 1.0f;
label2.innerShadowColor = [UIColor colorWithWhite:0.0f alpha:0.8f];
label2.innerShadowOffset = CGSizeMake(1.0f, 2.0f);
グラデーションカラーは,
label3.gradientStartColor = [UIColor redColor];
label3.gradientEndColor = [UIColor blackColor];
グラデーションの方向を指定する場合は始点の座標と終点の座標を指定します。また複数色指定したい場合はgradientColorsプロパティにUIColorの配列をセットします。
label4.gradientStartPoint = CGPointMake(0.0f, 0.0f);
label4.gradientEndPoint = CGPointMake(1.0f, 1.0f);
label4.gradientColors = [NSArray arrayWithObjects:
[UIColor redColor],
[UIColor yellowColor],
[UIColor greenColor],
[UIColor cyanColor],
[UIColor blueColor],
[UIColor purpleColor],
[UIColor redColor],
nil];
まとめ
テキストにフォントや色等の属性を指定したり,