TTT приписал Label Разноцветная помощь шрифтов - PullRequest
2 голосов
/ 21 мая 2011
-(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 разных цвета шрифта для отображения «лайков». Однако мой проект продолжает терпеть крах. Пример строки для форматирования: «Эндрю, Джон, Амос любит эту фотографию».

есть предложения? enter image description here

Ответы [ 2 ]

5 голосов
/ 26 июля 2012

Используйте вот так

[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; 
}];
0 голосов
/ 05 февраля 2013

Существует способ установить разные / несколько шрифтов и другие свойства, такие как цвет на Label, используя NSMutableAttributedStrin. Обратитесь к моему ответу на Несколько шрифтов и цветов для строки и отображения строки на этикетке

...