好多项目用到了这个 记录一下
TTTAttributedLabel的作者 Mattt
- (TTTAttributedLabel *)initlizerLinkLabel:(NSString *)content
WithUserName:(NSString *)userName
WithUserId:(NSString *)userId
{
TTTAttributedLabel *attributedLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(ScreenWidth - 250,10, 230, 60)];
attributedLabel.font = [UIFont systemFontOfSize:17.f];
attributedLabel.textColor = [UIColor darkGrayColor];
attributedLabel.lineBreakMode = NSLineBreakByWordWrapping;
attributedLabel.numberOfLines = 0;
attributedLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
//link字符样式
NSMutableDictionary *linkAttributes = [NSMutableDictionary dictionary];
[linkAttributes setValue:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
[linkAttributes setValue:(__bridge id)[UIColor redColor].CGColor forKey:(NSString *)kCTForegroundColorAttributeName];
UIFont *font = [UIFont fontWithName:@"DBLCDTempBlack" size:40.f];
[linkAttributes setValue:font forKey:(NSString *)kCTFontAttributeName];
attributedLabel.linkAttributes = linkAttributes;
attributedLabel.text = content;
NSRange range = [attributedLabel.text rangeOfString:userName];
[attributedLabel addLinkToURL:[NSURL URLWithString:userId] withRange:range];
//点击link样式
NSMutableDictionary *mutableActiveLinkAttributes = [NSMutableDictionary dictionary];
[mutableActiveLinkAttributes setValue:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
[mutableActiveLinkAttributes setValue:font forKey:(NSString *)kCTFontAttributeName];
[mutableActiveLinkAttributes setValue:(__bridge id)[UIColor redColor].CGColor forKey:(NSString *)kCTForegroundColorAttributeName];
attributedLabel.activeLinkAttributes = mutableActiveLinkAttributes;
attributedLabel.verticalAlignment = TTTAttributedLabelVerticalAlignmentBottom;
attributedLabel.textAlignment = NSTextAlignmentRight;
return attributedLabel;
}
附上下载: https://github.com/mattt/TTTAttributedLabel