TTTAttributedLabel 是 UILable 的改进,支持 NSAttributedStrings,据说用起来比较方便,但是需要增加很多代码,但比较灵活。
示例代码:
TTTAttributedLabel *label = [[[TTTAttributedLabel alloc] initWithFrame:CGRectZero] autorelease];
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:@"TTTCustomStrikeOut" value:[NSNumber numberWithBool:YES] range:strikeRange];
CFRelease(font);
}
return mutableAttributedString;
}];
源代码下载:
http://mattt.github.com/TTTAttributedLabel/
另外,也可以使用FXLabel来实现一些其它的效果,如下代码:
FXLabel *label = [[FXLabelalloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
label.backgroundColor = [UIColorclearColor];
label.font = [UIFontfontWithName:@"Helvetica-Bold"size:15];
label.text = [secondTitle objectAtIndex:i];
label.textColor = [UIColorgrayColor];
label.shadowColor = [UIColorcolorWithWhite:1.0falpha:0.8f];
label.shadowOffset = CGSizeMake(1.0f, 2.0f);
label.shadowBlur = 1.0f;
label.innerShadowColor = [UIColorcolorWithWhite:0.0falpha:0.8f];
label.innerShadowOffset = CGSizeMake(1.0f, 2.0f);
label.textAlignment = UITextAlignmentLeft;
[view addSubview:label];
源代码下载:https://github.com/nicklockwood/FXLabel