NSTextFieldCellを、各行に異なる形式の複数の行で表示する必要があります。
このようなもの:
1行目:タイトル
2行目:説明
NSTextFieldCellをサブクラス化しましたが、それを続行する方法がわかりません。
何か案は?
NSTextFieldCellを、各行に異なる形式の複数の行で表示する必要があります。
このようなもの:
1行目:タイトル
2行目:説明
NSTextFieldCellをサブクラス化しましたが、それを続行する方法がわかりません。
何か案は?
まず、これを実現するためにサブクラスを作成する必要はありません。NSTextFieldCell
これは、のサブクラスとしてNSCell
、NSTextFieldCell
を継承するため-setAttributedStringValue:
です。指定した文字列は、として表すことができますNSAttributedString
。次のコードは、通常の。を使用して目的のテキストを作成する方法を示していますNSTextField
。
MDAppController.h:
@interface MDAppController : NSObject <NSApplicationDelegate> {
IBOutlet NSWindow *window;
IBOutlet NSTextField *textField;
}
@end
MDAppController.m:
@implementation MDAppController
static NSDictionary *regularAttributes = nil;
static NSDictionary *boldAttributes = nil;
static NSDictionary *italicAttributes = nil;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
if (regularAttributes == nil) {
regularAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:[NSFont systemFontSize]],NSFontAttributeName,
nil] retain];
boldAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
[NSFont boldSystemFontOfSize:[NSFont systemFontSize]],NSFontAttributeName,
nil] retain];
NSFont *regFont = [NSFont userFontOfSize:[NSFont systemFontSize]];
NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSFont *oblique = [fontManager convertFont:regFont
toHaveTrait:NSItalicFontMask];
italicAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
oblique,NSFontAttributeName, nil] retain];
}
NSString *string = @"Line 1: Title\nLine 2: Description";
NSMutableAttributedString *rString =
[[[NSMutableAttributedString alloc] initWithString:string] autorelease];
[rString addAttributes:regularAttributes
range:[string rangeOfString:@"Line 1: "]];
[rString addAttributes:regularAttributes
range:[string rangeOfString:@"Line 2: "]];
[rString addAttributes:boldAttributes
range:[string rangeOfString:@"Title"]];
[rString addAttributes:italicAttributes
range:[string rangeOfString:@"Description"]];
[textField setAttributedStringValue:rString];
}
@end
これにより、次のようになります。
ここで、このテキストをどのように使用するかによって、いくつかの異なる方法でデザインを実装できます。NSTextView
あなたはよりもあなたのために働くかもしれないかどうかを調べたいと思うかもしれませんNSTextField
...
NSTextViewを使用することの何が問題になっていますか?