multi coloured string

  • Hi all,
    I have an issue with NSAttributedString -

    I am currently combining several strings into one string using
    -stringByAppendingString- and then converting this string into
    AttributedString for output -

    but each String has a distinct colour (user defined) which the
    NSAttributedString object does not recognize - it just colours the whole
    concatenated string the same colour based on the value it grabs from
    NSDictionary

    What is the best way to output a concatenated, multicolored string ?

    *Sample Code:*

    NSMutableDictionary *dictionary = [NSMutableDictionary alloc];

    function for user defined color on string - accepts NSString *temp

    {

    ...

    ...

    ...

    [dictionary setObject:[some color] forKey:NSForegroundColorAttributeName];

    [dictionary setObject:[some color] forKey:NSBackgroundColorAttributeName];

    }

    temp = [temp stringByAppendingString:newLine];

    attributesLine = [[NSAttributedString alloc] initWithString:temp
    attributes:dictionary];
  • On 11/7/07, Erfan Aleemullah <erfan.aleem...> wrote:
    > Hi all,
    > I have an issue with NSAttributedString -
    >
    > I am currently combining several strings into one string using
    > -stringByAppendingString- and then converting this string into
    > AttributedString for output -
    >
    > but each String has a distinct colour (user defined) which the
    > NSAttributedString object does not recognize - it just colours the whole
    > concatenated string the same colour based on the value it grabs from
    > NSDictionary
    >
    > What is the best way to output a concatenated, multicolored string ?

    Simple create an attributed string with each string range colored as needed...

    Your example code appends strings together and then creates an
    attributed string with what looks to be the last color specified,
    which is not what I think you want.

    Why not use NSMutableAttributed string? You would then create an
    NSAttributed string for each string with the color information for
    that string. Once you have that attributed string append it to your
    mutable attributed string.

    -Shawn
  • > I am currently combining several strings into one string using
    > -stringByAppendingString- and then converting this string into
    > AttributedString for output -
    >
    > but each String has a distinct colour (user defined) which the
    > NSAttributedString object does not recognize - it just colours the whole
    > concatenated string the same colour based on the value it grabs from
    > NSDictionary

      -stringByAppendingString: returns an NSString. How do you think your
    attributed string's attribute runs would be affected by this?

    > What is the best way to output a concatenated, multicolored string ?

      The best way? Dunno for sure, but the easy way: Create an
    NSMutableAttributedString and use -appendAttributedString: ...

    --
    I.S.
  • On 11/7/07, Shawn Erickson <shawnce...> wrote:
    > On 11/7/07, Erfan Aleemullah <erfan.aleem...> wrote:
    >> Hi all,
    >> I have an issue with NSAttributedString -
    >>
    >> I am currently combining several strings into one string using
    >> -stringByAppendingString- and then converting this string into
    >> AttributedString for output -
    >>
    >> but each String has a distinct colour (user defined) which the
    >> NSAttributedString object does not recognize - it just colours the whole
    >> concatenated string the same colour based on the value it grabs from
    >> NSDictionary
    >>
    >> What is the best way to output a concatenated, multicolored string ?
    >
    > Simple create an attributed string with each string range colored as needed...
    >
    > Your example code appends strings together and then creates an
    > attributed string with what looks to be the last color specified,
    > which is not what I think you want.
    >
    > Why not use NSMutableAttributed string? You would then create an
    > NSAttributed string for each string with the color information for
    > that string. Once you have that attributed string append it to your
    > mutable attributed string.

    On a side note I have the following categories to help with this...

    @implementation NSAttributedString (COLUtilities)
    + (id) stringWithString:(NSString*)string attributes:(NSDictionary*)attributes
    {
        if (attributes == nil) {
            return [[[self alloc] initWithString:string] autorelease];
        } else {
            return [[[self alloc] initWithString:string
    attributes:attributes] autorelease];
        }
    }
    @end

    @implementation NSMutableAttributedString (COLUtilities)
    - (void) appendString:(NSString*)string attributes:(NSDictionary*)attributes
    {
        [self appendAttributedString:[NSAttributedString
    stringWithString:string attributes:attributes]];
    }
    @end
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today