使用/ n在NSString中添加新行,但是如何将两行字符串居中?

时间:2017-02-06 14:34:31

标签: objective-c nsstring

 NSString *st = [NSString stringWithFormat:@"%lu \n", step];
        NSString *currentSteps = [st stringByAppendingString:NSLocalizedString(@"steps", "")];
 [_circleProgressBar setHintTextGenerationBlock:(^NSString *(CGFloat progress) {
            return [NSString stringWithFormat:@"%@", currentSteps];
        } )];

我现在有很多步骤,如下所示:
0
步骤

但是,如果0应该在字符串步骤的中心,那将是很好的。

1 个答案:

答案 0 :(得分:0)

您可以使用带有文本中心的段落创建NSAttributedString

NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
paragraphStyle.alignment = NSTextAlignmentCenter;

NSDictionary *attributes = @{ NSParagraphStyleAttributeName : paragraphStyle];

NSAttributedString *label = [[NSAttributedString alloc] initWithString:currentSteps attributes:attributes]