118 votes

iPhone - NSAttributedString ajouter l'alignement du texte ???

Comment puis-je ajouter du texte allignment attribut à un NSAttributedString centrer le texte?

Edit: Suis-je en train de faire quelque chose de mal? Il n'a pas seemt o modifier le allignment.

CTParagraphStyleSetting setting;
setting.spec = kCTParagraphStyleSpecifierAlignment;
setting.valueSize = kCTCenterTextAlignment;

CTParagraphStyleSetting settings[1] = {
    {kCTParagraphStyleSpecifierAlignment, sizeof(CGFloat), &setting},           
};

CTParagraphStyleRef paragraph = CTParagraphStyleCreate(settings, sizeof(setting));

NSMutableAttributedString *mutableAttributed = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedString];
[mutableAttributed addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:(NSObject*)paragraph ,(NSString*) kCTParagraphStyleAttributeName, nil] range:_selectedRange];

288voto

ejkujan Points 1211
 NSMutableParagraphStyle *paragrapStyle = NSMutableParagraphStyle.new;
 paragrapStyle.alignment                = NSTextAlignmentCenter;

 NSAttributedString *attributedString   = 
[NSAttributedString.alloc initWithString:@"someText" 
                              attributes:
         @{NSParagraphStyleAttributeName:paragrapStyle}];

95voto

ZeMoon Points 3982

J'étais à la recherche pour le même problème et a pu le centre d'aligner le texte dans une NSAttributedString de cette façon:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
[paragraphStyle setAlignment:NSTextAlignmentCenter];

NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:string];
[attribString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];

40voto

omz Points 38947

En tant que NSAttributedString est principalement utilisé pour la Base de Texte sur iOS, vous devez utiliser CTParagraphStyle au lieu de NSParagraphStyle. Il n'est pas mutable variante.

Par exemple:

CTTextAlignment alignment = kCTCenterTextAlignment;

CTParagraphStyleSetting alignmentSetting;
alignmentSetting.spec = kCTParagraphStyleSpecifierAlignment;
alignmentSetting.valueSize = sizeof(CTTextAlignment);
alignmentSetting.value = &alignment;

CTParagraphStyleSetting settings[1] = {alignmentSetting};

CFIndex settingCount = 1;
CTParagraphStyleRef paragraphRef = CTParagraphStyleCreate(settings, settingsCount);
NSDictionary *attributes = @{(__bridge id)kCTParagraphStyleAttributeName : (__bridge id)paragraphRef};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Hello World" attributes:attributes];

1voto

Verglas Points 401

Dans iOS 7, vous pouvez charger une NSAttributedString de la syntaxe HTML.
Donc, si vous souhaitez modifier l'alignement, vous pouvez essayer:

//HTML Syntax with text centered
NSString *style =  @"<html><head><style type=\"text/css\">body {font-size: 15px; font-family: Avenir; text-align: center;}</style></head><body>%TEXT%</body></html>";

NSString *finalString = [style stringByReplacingOccurrencesOfString:@"%TEXT%" withString:text];

NSData* data = [finalString dataUsingEncoding:NSUnicodeStringEncoding];

NSAttributedString *stringWithHTMLAttributes = [[NSAttributedString alloc] initWithData:data                                                                                options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}                                                                    documentAttributes:nil                                                                               error:nil];


[textView setAttributedText:stringWithHTMLAttributes];

Espère que ça aide

-5voto

mahendra Points 27
        [averagRatioArray addObject:[NSString stringWithFormat:@"When you respond Yes to %@ the average response to    %@ was %0.02f",QString1,QString2,M1]];
        [averagRatioArray addObject:[NSString stringWithFormat:@"When you respond No  to %@ the average response to    %@ was %0.02f",QString1,QString2,M0]];



        UIFont *font2 = [UIFont fontWithName:@"Helvetica-Bold" size:15];
        UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:12];

        NSMutableAttributedString *str=[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"When you respond Yes to %@ the average response to %@ was",QString1,QString2]];
        [str addAttribute:NSFontAttributeName value:font range:NSMakeRange(0,[@"When you respond Yes to " length])];
        [str addAttribute:NSFontAttributeName value:font2 range:NSMakeRange([@"When you respond Yes to " length],[QString1 length])];
        [str addAttribute:NSFontAttributeName value:font range:NSMakeRange([QString1 length],[@" the average response to " length])];
        [str addAttribute:NSFontAttributeName value:font2 range:NSMakeRange([@" the average response to " length],[QString2 length])];
        [str addAttribute:NSFontAttributeName value:font range:NSMakeRange([QString2 length] ,[@" was" length])];
       // [str addAttribute:NSFontAttributeName value:font2 range:NSMakeRange(49+[QString1 length]+[QString2 length] ,8)];
        [averagRatioArray addObject:[NSString stringWithFormat:@"%@",str]];

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X