Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSTextField添加行间距_Objective C_Cocoa_Nstextfield_Nsattributedstring - Fatal编程技术网

Objective c NSTextField添加行间距

Objective c NSTextField添加行间距,objective-c,cocoa,nstextfield,nsattributedstring,Objective C,Cocoa,Nstextfield,Nsattributedstring,我使用NSTextField而不是NSTextView来接收用户输入,但我需要自定义字体和文本颜色以及行距。我使用下面的代码,字体和颜色都可以,但我不知道如何设置行距 [self.titleField setTextColor:textColor]; [self.titleField setFont:bold14]; 我还使用NSAttributedString来解决这个问题: NSFont *bold14 = [NSFont boldSystemFontOfSize:14.0]; NSCol

我使用NSTextField而不是NSTextView来接收用户输入,但我需要自定义字体和文本颜色以及行距。我使用下面的代码,字体和颜色都可以,但我不知道如何设置行距

[self.titleField setTextColor:textColor];
[self.titleField setFont:bold14];
我还使用NSAttributedString来解决这个问题:

NSFont *bold14 = [NSFont boldSystemFontOfSize:14.0];
NSColor *textColor = [NSColor redColor];
NSMutableParagraphStyle *textParagraph = [[NSMutableParagraphStyle alloc] init];
[textParagraph setLineSpacing:10.0];

NSDictionary *attrDic = [NSDictionary dictionaryWithObjectsAndKeys:bold14, NSFontAttributeName, textColor, NSForegroundColorAttributeName, textParagraph, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:title attributes:attrDic]; 
[self.titleField setAllowsEditingTextAttributes:YES];
[self.titleField setAttributedStringValue:attrString];
上面的代码可以显示属性字符串,但是当我删除textfield中的字符串并开始输入时,这些单词没有任何属性


如何使用自定义字体、颜色和行距在NSTextField中输入字符串?

最好使用
NSTextField的
属性设置方法,而不是
NSAttributedString
,因为这样可以将设置发送到字段编辑器。每个文本字段都有一个
NSTextView
(大多数情况下)”;而字段编辑器就是进行编辑的

您的
NSAttributedString
没有粘滞,因为您只是告诉textfield临时显示该字符串。当字段编辑器弹出时,文本字段(单元格)会传递其自身的属性,如
textField.font
textField.textColor
,但决不会传递
NSAttributedString
的属性

最好使用
NSTextView
来使用
-setDefaultParagraphStyle
,因为从我看到的情况来看,您正在编辑多行。如果由于性能问题或其他原因,您不能这样做,那么:


子类
NSTextFieldCell
,因为这是所有
NSTextField
工作和重写的原因

- (NSText *)setUpFieldEditorAttributes:(NSText *)textObj

(在
NSCell
中声明)为字段编辑器设置所需的属性,以便您可以自己通过
-setDefaultParagraphStyle
(和字体等)向其发送行高值。(
textObj
是要设置的字段编辑器)。

谢谢您的回答,但我在NSText引用中找不到setDefaultParagraphStyle方法,这是因为尽管返回类型为
setUpFieldEditorAttributes:
NSText
,但字段编辑器实际上是一个
NSTextView