2011-07-10 9 views
1

は、私はこのようなリストを描きたい:ここ カスタム段落スタイルの文字列をCATextLayerに描画できますか?

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

は私のコードです:私はCATextLayerの文字列プロパティにattrStringを設定しますが、paragphスタイルが適用されていないその後
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]; 

結果。

答えて

1

はい、可能です。 initWithHTML*またはinitWithRTF*メソッドを使用してNSAttributedStringを作成できます。
プログラムでリストを作成する場合は、NSTextListを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]; 
関連する問題