ios objective-c nsattributedstring core-text

ios - NSAttributedString agrega alineación de texto



objective-c core-text (6)

¿Cómo puedo agregar un atributo de alineación de texto a un NSAttributedString para centrar el texto?

Editar: ¿Estoy haciendo algo mal? No parece cambiar la alineación.

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


Swift 3.0+

let titleParagraphStyle = NSMutableParagraphStyle() titleParagraphStyle.alignment = .center let titleFont = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline) let title = NSMutableAttributedString(string: "You Are Registered", attributes: [NSFontAttributeName:titleFont, NSForegroundColorAttributeName:UIColor.red, NSParagraphStyleAttributeName: titleParagraphStyle])

(respuesta original a continuación)

Swift 2.0+

let titleParagraphStyle = NSMutableParagraphStyle() titleParagraphStyle.alignment = .Center let titleFont = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline) let title = NSMutableAttributedString(string: "You Are Registered", attributes:[NSFontAttributeName:titleFont, NSForegroundColorAttributeName:UIColor.redColor(), NSParagraphStyleAttributeName: titleParagraphStyle])


Como NSAttributedString se usa principalmente con Core Text en iOS, debe usar CTParagraphStyle lugar de NSParagraphStyle . No hay variante mutable.

Por ejemplo:

CTTextAlignment alignment = kCTCenterTextAlignment; CTParagraphStyleSetting alignmentSetting; alignmentSetting.spec = kCTParagraphStyleSpecifierAlignment; alignmentSetting.valueSize = sizeof(CTTextAlignment); alignmentSetting.value = &alignment; CTParagraphStyleSetting settings[1] = {alignmentSetting}; size_t settingsCount = 1; CTParagraphStyleRef paragraphRef = CTParagraphStyleCreate(settings, settingsCount); NSDictionary *attributes = @{(__bridge id)kCTParagraphStyleAttributeName : (__bridge id)paragraphRef}; NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Hello World" attributes:attributes];


Estaba buscando el mismo problema y pude centrar el texto en un NSAttributedString de esta manera:

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


Swift 4 respuesta:

// Define paragraph style - you got to pass it along to NSAttributedString constructor let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = .center // Define attributed string attributes let attributes = [.paragraphStyle: paragraphStyle] let attributedString = NSAttributedString(string:"Test", attributes: attributes)


NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new; paragraphStyle.alignment = NSTextAlignmentCenter; NSAttributedString *attributedString = [NSAttributedString.alloc initWithString:@"someText" attributes: @{NSParagraphStyleAttributeName:paragraphStyle}];


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