ios 怎么给文字添加点击效果
2015-01-26 · 知道合伙人数码行家
知道合伙人数码行家
向TA提问 私信TA
源码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];
<br/>
本文来源于<b>大学IT网</b>