cocoanstextfield

Simple NSTextList question


I am trying to use NSTextList to display a numeric list in a multi-line NSTextField but it is not working as intended.

The sample code used is:

- (IBAction)displayResult:(id)sender
{
    NSTextList *list = [[NSTextList alloc] initWithMarkerFormat:self.markerFormat options:1]; // {Decimal} passed as marker format
    NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [paragraph setTextLists:[NSArray arrayWithObject:list]];
    [list release];
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:paragraph, NSParagraphStyleAttributeName, nil];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:self.inputString attributes:attributes] ; // input string is- George \n Thomas \n Ashok
    [self setOutputString:attrString]; 
    [attrString release];
    [paragraph release];
}

The input is -

George 

Thomas 

Ashok

The output should be -

1 George

2 Thomas

3 Ashok

but output it is showing is -

George 

Thomas 

Ashok

Can anyone suggest how to achieve the expected output?


Solution

  • Everything you are doing looks fine and ornately to me:) There is a problem with your input string Try this,

    NSTextList *list1 = [[NSTextList alloc] initWithMarkerFormat:@"{decimal}" options:0]; // {Decimal} passed as marker format
    NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [list1 setStartingItemNumber:1];
    [paragraph setTextLists:[NSArray arrayWithObject:list1]];
    
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:paragraph, NSParagraphStyleAttributeName, nil];
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"\t%@ Suhas \n \t%@ Devarshi \n \t%@ Rohith\n", [list1 markerForItemNumber:1],[list1 markerForItemNumber:2],[list1 markerForItemNumber:3]] attributes:attributes] ;
     [self.text setStringValue:attrString];//self.text is a NSTextField instance. kindly ignore the compiler warning:)