2012-11-03 28 views
7

我工作過UILabel。但setLineBreakMode已棄用。 我一直在使用NSAttributedString。 但UILabel setLineBreakMode是 之後,UILabel setNumberOfLines否則不起作用。新的NSAttributedString多行

前:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)]; 
label.text = @"XXXXXX"; 
memoLabel.textAlignment = UITextAlignmentLeft; 
memoLabel.numberOfLines = 2; 
memoLabel.lineBreakMode = UILineBreakModeTailTruncation; 
memoLabel.font = [UIFont systemFontOfSize:11]; 
memoLabel.backgroundColor = [UIColor clearColor]; 

IOS 6後:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; 
paragraphStyle.alignment = NSTextAlignmentLeft; 
NSAttributedString *string 
    = [[NSAttributedString alloc] initWithString:text 
            attributes:[NSDictionary 
               dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:11], 
               NSFontAttributeName, 
               paragraphStyle, NSParagraphStyleAttributeName,nil]]; 
[paragraphStyle release]; 
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(42.0f, 10.0f, 275.0f, 50.0f)]; 
label.attributedText = string; 
[string relase]; 

我想之前和顯示器後相同。 如何顯示多行?

+0

許多被你在這裏找到。 你見過嗎? – rbbtsn0w

回答

4

lineBreakMode屬性在iOS 6中不被棄用。它只是改變了常量的名字。舊的常量已被棄用,但仍然可用。即使您正在部署到較舊的iOS,也可以使用新常量,因爲常量只是枚舉值。舊名稱和新名稱具有相同的值。所以,只需設置memoLabel.lineBreakMode = NSLineBreakByTruncatingTail

您的示例代碼似乎沒有利用任何歸因於字符串的特定功能。如果你不需要一個屬性字符串,只要繼續使用一個純字符串即可。這仍然工作在IOS 6

2

使用NSLineBreakByTruncatingTail,而不是UILineBreakModeTailTruncation