How can I add text alignment attribute to an NSAttributedString to center the text?
Edit: Am I doing anything wrong? It doesn't seem to change the alignment.
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];
NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.alignment = NSTextAlignmentCenter;
NSAttributedString *attributedString =
[NSAttributedString.alloc initWithString:@"someText"
attributes:
@{NSParagraphStyleAttributeName:paragraphStyle}];
Swift 4.2
let paragraphStyle: NSMutableParagraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.center
let attributedString = NSAttributedString(string: "someText", attributes: [NSAttributedString.Key.paragraphStyle : paragraphStyle])