2017-04-20 8 views
0

J'essaie d'afficher une chaîne attributeString dans un UILabel et j'ai ajouté la propriété "firstLineHeadIndent" à ses attributs.assignedString utilisant l'attribut "firstLineHeadIndent" ne s'affiche pas correctement dans un UILabel

Avec 16 caractères de "国", il devrait se casser en deux lignes. Mais UILabel vient de me montrer 14 caractères en une seule ligne comme le montre l'image "pas correct avec 16 caractères".

Et il peut montrer correctement lors de l'utilisation de 17 caractères.Peut-on me dire pourquoi? Voici mon code:

- (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];} 

Non correct avec 16 caractères:

not correct with 16 characters

Afficher correctement avec 17 caractères:

show correctly with 17 characters

+0

u ur problème résolu –

Répondre

0

enter image description hereMise à jour Réponse

changez cette ligne: à

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]; 

solution 2:

supprimer le paragraphe: chose

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

encore ur problème auquel sont confrontés commentaires ci-dessous .. @ X.Tristan –

+0

UILabel n'a pas de changement après l'ajout de ces deux phrases .. minimumFontSize est dep indiqué dans iOS6.0. Et ajustsFontSizeToFitWidth ne fonctionne pas non plus. –

+0

vérifiez maintenant pour moi son affichage parfait –