2013-04-22 74 views
4

是否可以在UIRefreshControl標題中使用多行?每當我將\ n添加到NSAttributedString時,只會顯示第一行。我正在嘗試設置標題,並在下一行添加更多文字。那麼在UIRefreshControl中使用兩行文字是否有解決方法?UIRefreshControl歸因題目多行

這是當前的代碼顯示只有「標題下面」:

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 2013-04-22 12:25:50

+1

不起作用,文本顯示爲彼此相鄰。有人提供正確的答案嗎? – eter 2013-12-19 08:59:56

3

這裏是一個棘手方法:找到UILefreshControl中的UILabel並將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]; 
}