如何在属性中添加NSTextAlignmentCenter?

时间:2017-08-22 22:56:40

标签: objective-c macos

我想将NSTextAlignmentCenter添加到以下属性。怎么样?

[nssWord drawInRect:CGRectMake(200, 200, 500, 200) withAttributes: @{ NSFontAttributeName : [NSFont fontWithName:@"Arial Black" size:100.0] }];

尝试了这个...只是显示文字有效:

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:nssWord];


[string drawInRect:CGRectMake(200, 200, 500, 200)];

[string addAttribute:NSFontAttributeName
               value:[NSFont fontWithName:@"Arial Black" size:100.0]
               range:NSMakeRange(0, [nssWord length])];

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

[string addAttribute:NSParagraphStyleAttributeName
               value:paragraphStyle
               range:NSMakeRange(0, [nssWord length])];

1 个答案:

答案 0 :(得分:0)

制作NSMutableParagraphStyle,将其alignment属性设置为NSTextAlignmentCenter,然后将其添加到以NSParagraphStyleAttributeName为键的属性dict中。