2017-04-20 67 views
0

我試圖在UILabel中顯示屬性字符串,並且我向其屬性添加了「firstLineHeadIndent」屬性。使用屬性「firstLineHeadIndent」的屬性字符串在UILabel中無法正確顯示

具有16個「國」字符,它應該分成兩行。但是UILabel只顯示了14個字符,只有一行顯示在圖片「16個字符不正確」中。

它使用17個字符時可以正確顯示。可以告訴我爲什麼嗎? 這裏是我的代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 80)]; 
    label.numberOfLines = 0; 
    label.lineBreakMode = NSLineBreakByWordWrapping; 
    [self.view addSubview: label]; 

    NSMutableParagraphStyle *paraStyle = [NSMutableParagraphStyle new]; 
    paraStyle.firstLineHeadIndent = 40; 
    paraStyle.lineBreakMode = NSLineBreakByWordWrapping; 
    NSDictionary *attrs = @{ 
          NSParagraphStyleAttributeName:paraStyle, 
          NSFontAttributeName:[UIFont systemFontOfSize:17 weight:UIFontWeightBold], 
          NSKernAttributeName:@.9f 
          }; 
    label.attributedText = [[NSAttributedString alloc] initWithString:@"國國國國國國國國國國國國國國國國" attributes:attrs];} 

不可與16個字符正確:

not correct with 16 characters

顯示正確地17個字符:

show correctly with 17 characters

+0

ü解決問題烏爾下面 –

回答

0

enter image description here更新回答

改變這一行:以

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 80)]; 
    label.numberOfLines = 0; 
    label.lineBreakMode = NSLineBreakByCharWrapping; 
    label.adjustsFontSizeToFitWidth=YES; 
    label.backgroundColor=[UIColor redColor]; 
    label.textAlignment=NSTextAlignmentLeft; 
    [self.view addSubview: label]; 

    NSMutableParagraphStyle *paraStyle = [NSMutableParagraphStyle new]; 
    paraStyle.firstLineHeadIndent = 20; 
    paraStyle.lineBreakMode = NSLineBreakByWordWrapping; 
    NSDictionary *attrs = @{ 
          NSParagraphStyleAttributeName:paraStyle, 
          NSFontAttributeName:[UIFont systemFontOfSize:16 weight:UIFontWeightBold], 
          NSKernAttributeName:@.9f 
          }; 
    label.attributedText = [[NSAttributedString alloc] initWithString:@"國國國國國國國國國國國國國國國國" attributes:attrs]; 

解決方案2:

刪除的段落:事情

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 300, 80)]; 
    label.numberOfLines = 0; 

[email protected]"國國國國國國國國國國國國國國國國國國"; 
    label.lineBreakMode = NSLineBreakByWordWrapping; 
    label.adjustsFontSizeToFitWidth=YES; 
    label.backgroundColor=[UIColor redColor]; 
    label.textAlignment=NSTextAlignmentCenter; 
    [self.view addSubview: label]; 
+0

仍然烏爾面對問題評論.. @ X.Tristan –

+0

添加這兩個句子後,UILabel沒有變化。minimumFontSize is dep在iOS6.0中重新調整。而adjustsFontSizeToFitWidth也不起作用。 –

+0

現在檢查我的顯示完美 –

相關問題