我正在创建cocoa应用程序,其中我以NSTextField编程方式创建了这样的,
NSView *superView = [[NSView alloc] initWithFrame:NSMakeRect(0, 300, 1400, 500)];
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:NSMakeRect(180, 100, 1000, 300)];
[myTextField setStringValue:myText];
[myTextField setEditable:NO];
[myTextField setBezeled:NO];
[myTextField setDrawsBackground:NO];
[myTextField setSelectable:NO];
[myTextField setFont:[NSFont fontWithName:@"Futura" size:22]];
[superView addSubview:myTextField];
[superView setAutoresizesSubviews:YES];
[myTextField setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[self.window.contentView addSubview:superView];
Run Code Online (Sandbox Code Playgroud)
现在我想要文本的垂直对齐,它应该根据文本长度进行调整.
有人有什么建议吗?请分享你的建议:)
非常感谢..!!
在某个高度NSTextField中,如果文本的大小很小,则布局将如此

文本流向顶部,如何使文本中心像这样:

我一直在阅读有关如何在NSTextField上设置垂直对齐的各种选项.我希望文本显示在中心,并在Swift中以编程方式进行.以下是我目前看到的事情:
我在Swift中尝试过的一件事是设置以下属性:
textField.usesSingleLineMode = true
Run Code Online (Sandbox Code Playgroud)
有关垂直居中文本的最佳方法的任何提示将非常感谢!
我有一个NSTextFieldCell,我想用中间垂直对齐显示.感谢这里的旧问题和博客文章,我有两个有效的解决方案.
然而,这两种解决方案似乎都压低了我将细胞设置为右对齐的能力.任何人都可以帮助我使这些解决方案中的任何一种都支持这两种形式
以下是一个解决方案的代码:
@implementation MiddleAlignedTextFieldCell
- (NSRect)titleRectForBounds:(NSRect)theRect {
NSRect titleFrame = [super titleRectForBounds:theRect];
NSSize titleSize = [[self attributedStringValue] size];
titleFrame.origin.y = theRect.origin.y - .5 + (theRect.size.height - titleSize.height) / 2.0;
return titleFrame;
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect titleRect = [self titleRectForBounds:cellFrame];
[[self attributedStringValue] drawInRect:titleRect];
}
@end
Run Code Online (Sandbox Code Playgroud)
另一种解决方案是(从这个博客获得):
@implementation RSVerticallyCenteredTextFieldCell
- (NSRect)drawingRectForBounds:(NSRect)theRect
{
NSRect newRect = [super drawingRectForBounds:theRect];
if (mIsEditingOrSelecting == NO)
{
// Get our ideal size for current text
NSSize textSize = [self cellSizeForBounds:theRect];
// …Run Code Online (Sandbox Code Playgroud) 我有一个NSTableView,我用以下代码更改行的高度:
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
return 32;
}
Run Code Online (Sandbox Code Playgroud)
问题是,我的文本单元仍然与单元格顶部对齐,使其看起来很尴尬.我还没弄清楚如何垂直居中.还有其他人找到了解决方案吗?
我一直在寻找一个解决方案,使我的NSTextField底部对齐,我发现了这个,并根据我的需要进行了调整.所以现在我有了这个自定义的NSTextFieldCell但是如何告诉我的NSTextFields使用这个类(以编程方式)?
如何添加填充NSTableCellView?
我希望内容的填充量为10px,类似于纯HTML中的内容.(所有面).文本应在中间垂直居中.
目前我正在NSTextFieldCell的子类中执行此操作.但这似乎不正常.
编辑文本时,编辑文本字段不使用textfieldcell中的填充.
图2:


这是我目前的代码,(子类NSTextFieldCell)
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect titleRect = [self titleRectForBounds:cellFrame];
NSAttributedString *aTitle = [self attributedStringValue];
if ([aTitle length] > 0) {
[aTitle drawInRect:titleRect];
}
}
- (NSRect)titleRectForBounds:(NSRect)bounds
{
NSRect titleRect = bounds;
titleRect.origin.x += 10;
titleRect.origin.y = 2;
NSAttributedString *title = [self attributedStringValue];
if (title) {
titleRect.size = [title size];
} else {
titleRect.size = NSZeroSize;
}
// We don't want the width of the string going outside the cell's …Run Code Online (Sandbox Code Playgroud) cocoa ×6
objective-c ×4
nstableview ×2
nstextfield ×2
xcode ×2
appkit ×1
macos ×1
swift ×1
xcode4.2 ×1