2013-02-23 58 views
1

他,当我画文成我的看法是这样的:线对齐在NSFont /的NSView

NSMutableDictionary *textAttrib = [[NSMutableDictionary alloc] init]; 
[textAttrib setObject:[NSFont fontWithName:@"Helvetica Light" size:15] forKey:NSFontAttributeName]; 
[textAttrib setObject:[NSColor grayColor] forKey:NSForegroundColorAttributeName]; 
[mouseString drawAtPoint:textPoint withAttributes:textAttrib]; 

我如何可以改变文本的对齐方式?我只需要简单的左/右对齐,并且只能在文本框中找不到关于该对象的任何信息。谢谢!

回答

1

有没有这种与[NSString drawAtPoint:withAttributes:]方法对齐的概念,这是有道理的,如果你考虑它。对齐是字符串相对于控件内容的概念(典型地为NSTextField)。

但是,您可以尝试[NSString drawInRect:withAttributes:],该概念确实存在。看到这SO question

UPDATE:这个问题也不是没有通过@justin给出的(现已删除)回答,这表明如何设置在NSAttributedString文本对齐方式。两者都是正确的答案,但都不完整。

+0

@justin请取消删除您的答案,因为它表明参与制定对准机制。 – trojanfoe 2013-02-23 13:09:33

+0

试着回答:运行,但NSLeftTextAlignment或NSRightTextAlignment没有区别。 – fw2601 2013-02-23 13:13:35

+0

@ fw2601并尝试过*与我的答案结合* – trojanfoe 2013-02-23 13:16:34

1

您缺少属性:NSParagraphStyleAttributeName,它是NSParagraphStyle属性。如果你创建一个NSParagraphStyle对象,可以设置该属性:

@property(readonly) NSTextAlignment alignment; // readwrite for NSMutableParagraphStyle 

你的情况,你应该把它设置为NSTextAlignmentLeft,或NSTextAlignmentRight。

NSMutableParagraphStyle* style=[[NSMutableParagraphStyle alloc]init]; 
style.alignment= NSTextAlignmentLeft; 
[textAttrib setObject: style forKey: NSParagraphStyleAttributeName];