web-dev-qa-db-fra.com

La définition de la police sur NSAttributedString dans UITextView ne tient pas compte de l'interligne

J'essaie de définir une chaîne attribuée à une UITextView dans iOS 6. Le problème est que, si j'essaie de définir la propriété de police sur la chaîne attribuée, l'interligne est ignoré. Toutefois, si je ne définit pas la police et que la police par défaut est utilisée, l'interligne fonctionne.

NSString *string = @" Hello \n world";
attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

paragraphStyle.minimumLineHeight = 50;
// setting the font below makes line spacing become ignored
[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

mainTextView.attributedText = attrString;

Une idée de ce qui se passe?

72
Snowman

Guide de programmation attribué:

UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:14.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"strigil" attributes:attrsDictionary];

Mise à jour: j'ai essayé d'utiliser la méthode addAttribute: Dans ma propre application, mais cela ne semblait pas fonctionner sur le simulateur iOS 6:

NSLog(@"%@", textView.attributedText);

Le journal semble afficher correctement les attributs ajoutés, mais la vue sur le simulateur iOS n'était pas affichée avec les attributs.

102
maxeng

J'ai trouvé votre question parce que je me battais aussi avec NSAttributedString. Pour moi, les méthodes beginEditing et endEditing ont été efficaces, comme indiqué dans Modification d'une chaîne attribuée . En dehors de cela, le lineSpacing est défini avec setLineSpacing sur le paragraphStyle.

Donc, vous voudrez peut-être essayer de changer votre code pour:

NSString *string = @" Hello \n world";
attrString = [[NSMutableAttributedString alloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];

[paragraphStyle setLineSpacing:20]  // Or whatever (positive) value you like...    
[attrSting beginEditing];

[attrString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, string.length)];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, string.length)];

[attrString endEditing];

mainTextView.attributedText = attrString;

N'a pas testé ce code exact cependant, mais le mien a presque la même apparence.

MODIFIER:

En attendant, je l'ai testé et, corrigez-moi si je me trompe, le - beginEditing et - endEditing les appels semblent avoir toute une importance.

24
Jan Nash
//For proper line spacing

NSString *text1 = @"Hello";
NSString *text2 = @"\nWorld";
UIFont *text1Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:10];
NSMutableAttributedString *attributedString1 =
[[NSMutableAttributedString alloc] initWithString:text1 attributes:@{ NSFontAttributeName : text1Font }];
NSMutableParagraphStyle *paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle1 setAlignment:NSTextAlignmentCenter];
[paragraphStyle1 setLineSpacing:4];
[attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [attributedString1 length])];

UIFont *text2Font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:16];
NSMutableAttributedString *attributedString2 =
[[NSMutableAttributedString alloc] initWithString:text2 attributes:@{NSFontAttributeName : text2Font }];
NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle2 setLineSpacing:4];
[paragraphStyle2 setAlignment:NSTextAlignmentCenter];
[attributedString2 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle2 range:NSMakeRange(0, [attributedString2 length])];

[attributedString1 appendAttributedString:attributedString2];
3
Nitin Mehta

Il y avait un bogue dans iOS 6, qui faisait que la hauteur de ligne était ignorée lorsque la police était définie. Voir la réponse à l'espacement des lignes NSParagraphStyle est ignoré et une analyse de bogue plus longue en Radar: UITextView ignore la hauteur de ligne minimale/maximale dans la chaîne attribuée .

3
Palimondo

Vous pouvez utiliser cet exemple et changer son implémentation comme ceci:

[self enumerateAttribute:NSParagraphStyleAttributeName
                 inRange:NSMakeRange(0, self.length)
                 options:0
              usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
                  NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

                  //add your specific settings for paragraph
                  //...
                  //...

                  [self removeAttribute:NSParagraphStyleAttributeName range:range];
                  [self addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
              }];
1
landonandrey