首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >换行NSButton标题

换行NSButton标题
EN

Stack Overflow用户
提问于 2009-03-22 14:57:15
回答 8查看 7.4K关注 0票数 9

当NSButton标题的宽度大于按钮宽度时,有没有办法让它换行,而不是被裁剪?

我正在尝试一个带有文本的单选按钮,该文本可以很长并且有多行。我想让它工作的一种方法是有一个NSRadioButton类型的NSButton,但不能让多行文本工作。

也许我最好的选择是让一个NSButton后跟一个带有mouseDown委托函数的NSTextView来触发NSButton状态?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2009-03-22 15:31:14

我不相信你能做到。你必须将NSButtonCell子类化才能添加对它的支持。

也就是说,在一个按钮上有多行文本通常不是一个好主意。A button label should concisely represent the action performed:

按钮上的标签应该是动词或动词短语,用于描述按钮执行的操作-保存、关闭、打印、删除、更改密码等。如果按钮作用于单个设置,请尽可能明确地标记该按钮;“选择图片…例如,“,比”Choose…“更有帮助。因为按钮会立即启动操作,所以不需要在标签中使用“Now”(例如,Scan Now)。

你想做什么?

票数 7
EN

Stack Overflow用户

发布于 2017-02-12 19:50:43

m incredibly late, but I still feel obliged to share what I已经找到了。

在分配给实际的按钮之前,只需在按钮标题前后添加一个换行符--瞧!它现在会自动包装。

这种方法的缺点是,由于我不知道的原因,在OS的某个版本上编译的应用程序在较新版本上运行时,shift按钮标题会下降一行。

票数 6
EN

Stack Overflow用户

发布于 2013-07-17 08:07:05

这里是我需要多行按钮的理由:我正在为IBM701编写一个模拟器,其中包括前面板,并且,感谢他们,前面板的设计者使用了多行标签。这是我的代码。您只需要子类NSButtonCell (而不是NSButton),并且只需要覆盖一个方法。

代码语言:javascript
复制
// In Xcode 4.6 (don't know about earlier versions): Place NSButton, then double-click it
// and change class NSButtonCell to ButtonMultiLineCell.

@interface ButtonMultiLineCell : NSButtonCell

@end

@implementation ButtonMultiLineCell

- (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
{
    NSAttributedString *as = [[NSAttributedString alloc] initWithString:[title.string stringByReplacingOccurrencesOfString:@" " withString:@"\n"]];
    NSFont *sysFont = [NSFont systemFontOfSize:10];
    NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
    [paragraphStyle setAlignment:NSCenterTextAlignment];
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
      sysFont, NSFontAttributeName,
      paragraphStyle, NSParagraphStyleAttributeName,
      nil];
    NSSize textSize = [as.string sizeWithAttributes:attributes];
    NSRect textBounds = NSMakeRect(0, 0, textSize.width, textSize.height);
    // using frame argument seems to produce text in wrong place
    NSRect f = NSMakeRect(0, (controlView.frame.size.height - textSize.height) / 2, controlView.frame.size.width, textSize.height);
    [as.string drawInRect:f withAttributes:attributes];
    return textBounds; // not sure what rectangle to return or what is done with it
}

@end
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/671136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档