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

NSParagraphStyleno estaba disponible en iOS en ese entonces.Rápido 4.2
fuente
NSTextAlignmentJustified. ¿puedes decir por qué? y ¿cómo puedo establecer la alineación como justificada?NSTextAlignmentJustified. ¿Es un error de iOS7?NSBaselineOffsetAttributeName : @0al Diccionario de atributos.NSAttributedString.alloc?Estaba buscando el mismo problema y pude centrar la alineación del texto en un NSAttributedString de esta manera:
fuente
NSMakeRange(0, [string length])Representa la cadena completa.Swift 4.0+
Swift 3.0+
(respuesta original a continuación)
Swift 2.0+
fuente
Respuesta rápida 4 :
fuente
En Swift 4:
fuente
fuente