1

このようなリストを描きたい:

 1. List item 1
 2. List item 2
 3. List item 3

これが私のコードです:

NSTextList *list = [[NSTextList alloc] initWithMarkerFormat:@"{decimal}" options:0];
NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraph setTextLists:[NSArray arrayWithObject:list]];
[list release];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:paragraph, NSParagraphStyleAttributeName, nil];
NSString *string = @"List item 1\nList item 2\nList item 3"
NSMutableAttributedString *attrString = [[[NSMutableAttributedString alloc] initWithString:string attributes:attributes] autorelease];
[paragraph release];

次に、attrStringをCATextLayerのstringプロパティに設定しましたが、paragphスタイルが結果に適用されません。

4

1 に答える 1

1

はい、可能です。initWithHTML*またはinitWithRTF*メソッドを使用してNSAttributedStringを作成できます。
プログラムでリストを作成する場合は、NSTextTabをNSTextListと組み合わせて使用​​できます。ただし、文字列を手動でフォーマットする必要があります。

NSTextList *list = [[NSTextList alloc] initWithMarkerFormat:@"square" options:0];
NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraph setTextLists:[NSArray arrayWithObject:list]];
NSTextTab *t1 = [[NSTextTab alloc] initWithType:0 location:11.0f];
NSTextTab *t2 = [[NSTextTab alloc] initWithType:0 location:36.0f];
[paragraph setTabStops:[NSArray arrayWithObjects:t1, t2, nil]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:paragraph, NSParagraphStyleAttributeName, nil];
NSString *string = @"\t▪\tList item 1\n\t▪\tList item 2\n\t▪\tList item 3";
NSMutableAttributedString *attrString = [[[NSMutableAttributedString alloc] initWithString:string attributes:attributes] autorelease];
[paragraph release];
[list release];
[t1 release];
[t2 release];
于 2011-08-23T09:33:15.977 に答える