我有一个带背景的文本字段,但为了使其看起来正确,文本字段需要在其左侧有一些填充,类似于NSSearchField。我将如何给文本字段填充左侧?将填充添加到文本字段的“左边”
3
A
回答
7
使用覆盖drawingRectForBounds:的自定义NSTextFieldCell。让它尽可能多地嵌入矩形,然后将新矩形传递给[super drawingRectForBounds:]以获得正常的填充,并返回该调用的结果。
9
smorgan的回答指向了正确的方向,但我花了相当长的一段时间才弄清楚如何恢复自定义文本框显示背景色的能力 - 您必须在自定义单元上调用setBorder:YES
。
这是为时已晚,以帮助约书亚,但这里的你如何实现自定义单元格:
#import <Foundation/Foundation.h>
// subclass NSTextFieldCell
@interface InstructionsTextFieldCell : NSTextFieldCell {
}
@end
#import "InstructionsTextFieldCell.h"
@implementation InstructionsTextFieldCell
- (id)init
{
self = [super init];
if (self) {
// Initialization code here. (None needed.)
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (NSRect)drawingRectForBounds:(NSRect)rect {
// This gives pretty generous margins, suitable for a large font size.
// If you're using the default font size, it would probably be better to cut the inset values in half.
// You could also propertize a CGFloat from which to derive the inset values, and set it per the font size used at any given time.
NSRect rectInset = NSMakeRect(rect.origin.x + 10.0f, rect.origin.y + 10.0f, rect.size.width - 20.0f, rect.size.height - 20.0f);
return [super drawingRectForBounds:rectInset];
}
// Required methods
- (id)initWithCoder:(NSCoder *)decoder {
return [super initWithCoder:decoder];
}
- (id)initImageCell:(NSImage *)image {
return [super initImageCell:image];
}
- (id)initTextCell:(NSString *)string {
return [super initTextCell:string];
}
@end
(如果像约书亚,你只需要在左边的插图,离开origin.y和高度是,与相同的量添加到该宽度 - 不双 - 你做到origin.x)
分配这样的定制单元,在窗口/视图控制器的awakeFromNib方法,其拥有文本框:
// Assign the textfield a customized cell, inset so that text doesn't run all the way to the edge.
InstructionsTextFieldCell *newCell = [[InstructionsTextFieldCell alloc] init];
[newCell setBordered:YES]; // so background color shows up
[newCell setBezeled:YES];
[self.tfSyncInstructions setCell:newCell];
[newCell release];
相关问题
- 1. 查询填充0字段的左边
- 2. 从文本字段添加entires到UITableView自动填充
- 3. 在swift中添加填充到UITextField的左边距
- 4. 从左边添加文字
- 5. 将零填充添加到字符串
- 6. 在猪左边填充字符串
- 7. 将文本字段验证添加到多个文本字段
- 8. phpmyadmin将文本添加到字段
- 9. phpmyadmin将文本添加到空字段?
- 10. 将字段值添加到文本框
- 11. MySQL:将文本添加到字段
- 12. 动画文字填充从左到右
- 13. 将按钮添加到UISearchBar的左边
- 14. JavaScript将默认值填充到模糊的文本字段中
- 15. 通过添加到值从LiveCycle中的下拉菜单填充文本字段
- 16. 左边缘文本字段为UIUI
- 17. JS脚本来填充文本字段
- 18. 为什么jQuery append()为“ajaxed”数据添加左边距/填充?
- 19. 如何将字段添加到在信封创建时填充的文档
- 20. 如何在Java中将左填充的零添加到数字中?
- 21. 将左边距添加到UITextField
- 22. Xcode:如何添加边距/填充标签文本?
- 23. 如何将自动填充添加到蒙面文本框?
- 24. 如何在javascript中添加填充 - 左?
- 25. 将“填充”添加到div的外部?
- 26. 将填充和边框添加到UIImageView中
- 27. JQuery添加填充到原始填充
- 28. python pptx将边框添加到文本
- 29. 添加Excel列并填充文本
- 30. Firefox为版面添加边距/填充
我明白了,我会在方法中定义多少内容,或者是否存在一些示例代码? – Joshua 2009-07-27 16:47:30
如果您不熟悉NSRect,请尝试http://www.cocoadev.com/index.pl?NSRect。 – smorgan 2009-07-27 18:02:16