源码WPAttributedMarkup, WPAttributedMarkup能给Label中某关键字添加文字效果或点击事件。Label中的某个关键字词可以改变字体的特性如颜色、加粗,下划线等,也可以为某个关键字词添加点击事件。
<ignore_js_op>class="zoom" src="/Upload/Images/2014111211/1916AE6842DDEA5C.png" alt="" width="476" />
使用方法:
通过创建不同的style的字典后就可以对label的属性进行设置,示例如下:
// Example using fonts and colours
NSDictionary* style1 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:18.0],
@"bold":[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],
@"red": [UIColor redColor]};
// Example using arrays of styles, dictionary attributes for underlining and image styles
NSDictionary* style2 = @{@"body" :
@[[UIFont fontWithName:@"HelveticaNeue-Bold" size:18.0],
[UIColor darkGrayColor]],
@"u": @[[UIColor blueColor],
@{NSUnderlineStyleAttributeName : @(kCTUnderlineStyleSingle|kCTUnderlinePatternSolid)}
],
@"thumb":[UIImage imageNamed:@"thumbIcon"] };
// Example using blocks for actions when text is tapped. Uses the 'link' attribute to style the links
NSDictionary* style3 = @{@"body":[UIFont fontWithName:@"HelveticaNeue" size:22.0],
@"help":[WPAttributedStyleAction styledActionWithAction:^{
NSLog(@"Help action");
}],
@"settings":[WPAttributedStyleAction styledActionWithAction:^{
NSLog(@"Settings action");
}],
@"link": [UIColor orangeColor]};
self.label1.attributedText = [@"Attributed Bold Red text" attributedStringWithStyleBook:style1];
self.label2.attributedText = [@"[td] Multiple styles text [td] " attributedStringWithStyleBook:style2];
详细说明:http://ios.662p.com/thread-2190-1-1.html