源碼WPAttributedMarkup, WPAttributedMarkup能給Label中某關鍵字添加文字效果或點擊事件。Label中的某個關鍵字詞可以改變字體的特性如顏色、加粗,下劃線等,也可以為某個關鍵字詞添加點擊事件。
<ignore_js_op>
使用方法:
通過創建不同的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 = [@"AttributedBoldRedtext" attributedStringWithStyleBook:style1];
self.label2.attributedText = [@"[td]Multiplestylestext[td]" attributedStringWithStyleBook:style2];
詳細說明:http://ios.662p.com/thread-2190-1-1.html