2015-08-25 37 views
0

在現場有一個iPhone 4和一個奇怪的問題,UILabel不顯示任何文本。我在iPhone 4S + iOS 7模擬器上測試了它,它工作正常。UILabel.attributedText不顯示在iPhone 4 + iOS 7.0.3上

代碼:

NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[colLabel.text copy]]; 
[attributeString addAttribute:NSUnderlineStyleAttributeName 
         value:[NSNumber numberWithInt:1] 
         range:(NSRange){0,[attributeString length]}]; 
colLabel.text = nil; 
colLabel.attributedText = [attributeString copy]; 
colLabel.font = [UIFont boldSystemFontOfSize:12]; 
+0

請參考以下鏈接: - http://stackoverflow.com/questions/19127828/ios-7-bug-nsattributedstring-does-not-appear http://stackoverflow.com/questions/3482346/how-do-you-use-nsattributedstring – poojathorat

+0

上面的代碼在大多數情況下工作正常。在iPhone 4上用NSUnderlineStyleAttributeName奇怪。我試圖刪除底線屬性,它可以繪製。一旦加回來,不再工作。所以這是NSUnderlineStyleAttributeName問題。而且,你的鏈接不能解決我的問題。 – Wingzero

+0

想通了!上帝大壩* iPhone 4在7.0.2上,只是越野車而已。 – Wingzero

回答

0

我已經檢查。它在iPhone 4上展示,可能還有別的東西。乾淨的構建,並從設備中刪除,然後再次運行

+0

不,我嘗試刪除下面的行屬性,它可以繪製。一旦加回來,不再工作。所以這是NSUnderlineStyleAttributeName問題。 – Wingzero

+0

僅供參考,我的iPhone 4的iOS版本是7.1.2。您可以通過傳遞靜態文本來嘗試:NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc] initWithString:@「test」]; – Bhanupriya

+0

不工作,因爲我說 – Wingzero

0

我一直在玩了一段時間的屬性文本,並找出新的東西:

好像在iOS上的7.0.x,NSUnderlineStyleAttributeName不玩好其他屬性如顏色或字體,一旦它們被捆綁在一起,它就不會顯示文本。只有那些具有下劃線樣式實際上可以繪製文本象下面這樣:

NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:colLabel.text 
               attributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)}]; 
colLabel.attributedText = attrStr; 

但是,一旦你加入類似

colLabel.backgroundColor = [UIColor greenColor];

colLabel.font = [UIFont boldSystemFontOfSize:12];

它只是不會顯示除非你做了兩處更改:將一個換行符附加到原始字符串,並將標籤的numberOfLines設置爲2.

,如:

NSAttributedString* attrStr = 
    [[NSAttributedString alloc] initWithString:@"TEST\n" // <--- 
    attributes: 
     @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle), 
     NSParagraphStyleAttributeName:paragraph}]; 

UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)]; 
myLabel.backgroundColor = [UIColor greenColor]; 
myLabel.attributedText = attrStr; 
[myLabel sizeToFit]; 

myLabel.numberOfLines = 2; // <--- 
相關問題