2013-04-22 2 views
4

Возможно ли использовать несколько строк в заголовке UIRefreshControl? Всякий раз, когда я добавляю \ n к NSAttributedString, будет отображаться только первая строка. Я пытаюсь установить заголовок, а на следующей строке - еще текст. Итак, есть ли способ обхода двух строк текста в UIRefreshControl?UIRefreshControl attributedTitle несколько строк

Это текущий код, где только "Title Here" отображается:

self.refreshControl = [[UIRefreshControl alloc] init]; 

NSString *title = @"Title Here"; 
NSString *subText = @"Subtext Here"; 
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@",title,subText]]; 

[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:20.0f] range:NSMakeRange(0, [title length])]; 
[attString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:14.0f] range:NSMakeRange([title length],[subText length])]; 

[attString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [title length])]; 
[attString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange([title length], [subText length])]; 

self.refreshControl.attributedTitle = attString; 

ответ

1

Этот фрагмент кода будет работать

NSString *title = @"Title Here"; 
NSString *subText = @"Subtext Here"; 

NSMutableAttributedString *titleAttString = [[NSMutableAttributedString alloc] initWithString:title]; 
NSMutableAttributedString *subTitleAttString = [[NSMutableAttributedString alloc] initWithString:subText]; 

[titleAttString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:20.0f] range:NSMakeRange(0, [title length])]; 
[subTitleAttString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:14.0f] range:NSMakeRange(0,[subTitle length])]; 

[titleAttString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [title length])]; 
[subTitleAttString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, [subTitle length])]; 

[titleAttString appendAttributedString:subTitleAttString]; 

self.refreshControl.attributedTitle = titleAttString; 
+1

Это просто показывает их рядом друг с другом. – ggfela

+1

Не работает, текст отображается рядом друг с другом. Есть ли у кого-то правильный ответ? – eter

3

здесь является сложно метод: найти UILabel в UIRefreshControl и set numberOfLines = 0.

UILabel *titleLabel = [[[[self.refreshControl subviews] firstObject] subviews] lastObject]; 
if (titleLabel) { 
    titleLabel.numberOfLines = 0; 

    NSString title = @"Pull to Refresh.\nUpdated Time: 09:30"; // \n for new line. 
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:title]; 
} 
Смежные вопросы