TTT归因于标签多色字体帮助 [英] TTT attributed Label Multi- colored Font help
本文介绍了TTT归因于标签多色字体帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
-(UITableViewCell*) makeLikeCell
{
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SpotlightLikesTableViewCell" owner: self options: nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
UITableViewCell *cell = [topLevelObjects objectAtIndex:0];
NSString *likeText = [likesAndComments objectAtIndex: kLikeRow];
TTTAttributedLabel *likeLabel = [[[TTTAttributedLabel alloc] initWithFrame:CGRectZero] autorelease];
likeLabel = (TTTAttributedLabel*) [cell viewWithTag: kCaptionTag];
likeLabel.textColor = [UIColor blueColor];
likeLabel.lineBreakMode = UILineBreakModeWordWrap;
[likeLabel setFont: [UIFont fontWithName: @"Helvetica Neue" size: 10]];
[likeLabel setText: likeText afterInheritingLabelAttributesAndConfiguringWithBlock: ^NSAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange colorRange = [[mutableAttributedString string] rangeOfString: @" like this photo." options: NSCaseInsensitiveSearch];
[mutableAttributedString addAttribute:(NSString *) kCTForegroundColorAttributeName value:(id)[[UIColor grayColor] CGColor] range:colorRange];
return mutableAttributedString; }];
return cell;
}
我在项目中添加了TTT属性标签.我想要2种不同的字体颜色来显示喜欢".但是我的项目一直崩溃.要格式化的示例字符串为:安德鲁,约翰,阿莫斯都喜欢这张照片."
I added the TTT attributed label to my project. I want to have 2 different font colors to display the "likes". However my project keeps crashing. A sample string to be formatted would be: "andrew, john, amos likes this photo."
有什么建议吗?
推荐答案
像这样使用
[likeLabel setText:likeText afterInheritingLabelAttributesAndConfiguringWithBlock: ^(NSMutableAttributedString *mutableAttributedString) {
NSRange colorRange = [likeText rangeOfString: @"like this photo." options: NSCaseInsensitiveSearch];
if (colorRange.location != NSNotFound) {
[mutableAttributedString addAttribute:(NSString *) kCTForegroundColorAttributeName value:(id)[[UIColor grayColor] CGColor] range:colorRange];
}
return mutableAttributedString;
}];
这篇关于TTT归因于标签多色字体帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文