xcode 如何在视图中居中 NSTextField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10868480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:28:23  来源:igfitidea点击:

How to center a NSTextField in a view

objective-cxcodecocoainterface-buildernstextfield

提问by Elftheitroados

i've a NSTextField (label) centered with IB in a view with the text "__DRAG_AND_DROP_YOUR_MEDIA_HERE". In my controller a use NSLocalizedString to translate the text :

我有一个以 IB 为中心的 NSTextField(标签),在一个带有文本“__DRAG_AND_DROP_YOUR_MEDIA_HERE”的视图中。在我的控制器中,使用 NSLocalizedString 来翻译文本:

English.lproj/Localizable.strings :

英语.lproj/Localizable.strings :

"__DRAG_AND_DROP_YOUR_MEDIA_HERE" = "Drag & Drop your media here"

French.lproj/Localizable.strings :

法语.lproj/Localizable.strings :

"__DRAG_AND_DROP_YOUR_MEDIA_HERE" = "Déposer vos média ici"

Only the initial text "__DRAG_AND_DROP_YOUR_MEDIA_HERE" is centerred in the view. The translated text is not centerrd (depending on is lenght). All the translated text start at the same absciss x than the orignal text. How can i fix this issue ?

只有初始文本“__DRAG_AND_DROP_YOUR_MEDIA_HERE”在视图中居中。翻译的文本不是 centerrd(取决于长度)。所有翻译后的文本都从与原始文本相同的横坐标 x 开始。我该如何解决这个问题?

PS: I don't want to play with setFrame, height, width just for a simple point like this. I guess there's another method to do this.

PS:我不想仅仅为了这样一个简单的点而使用 setFrame、height、width。我想还有另一种方法可以做到这一点。

Thanks.

谢谢。

回答by Ken Thomases

Set the text field's alignment to NSCenterTextAlignment. You can do this from IB or programmatically using -setAlignment:.

将文本字段的对齐方式设置为NSCenterTextAlignment。您可以从 IB 或以编程方式使用-setAlignment:.