Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

oneeyeman

macrumors newbie
Original poster
Nov 14, 2012
16
0
Hi, ALL,
I am just wondering if its possible to have a RichText control which supports different color for the text/background and left indentation on the paragraph.

I tried it but failed.

Could someone please explain how to do it?

I am using NSMutableParagraphStyle to work with the indentation and using setHeadIndent()/setFirstLineHeadIndent() to work with indentation.
For selecting the colors I'm using the dictionary of typing attributes.

When I run the code it is either coloring or indentation, but not both.
Unfortunately I'm currently at work and don't have my Mac with me. I may post some code I tried later.

As promised here is what I have right now:

Code:
        NSMutableDictionary* const
            attrs = [NSMutableDictionary dictionaryWithCapacity:3];
        if ( style.HasFont() )
            [attrs setValue:style.GetFont().OSXGetNSFont() forKey:NSFontAttributeName];
        if ( style.HasBackgroundColour() )
            [attrs setValue:style.GetBackgroundColour().OSXGetNSColor() forKey:NSBackgroundColorAttributeName];
        if ( style.HasTextColour() )
            [attrs setValue:style.GetTextColour().OSXGetNSColor() forKey:NSForegroundColorAttributeName];
        if( style.HasLeftIndent() )
        {
            paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
            [paragraphStyle setHeadIndent: style.GetLeftIndent() * mm2pts / 10.0];
            [paragraphStyle setFirstLineHeadIndent: style.GetLeftIndent() * mm2pts / 10];
            NSString *str = [[m_textView textStorage] string];
            NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString: str];
            [attrString addAttribute:NSParagraphStyleAttributeName value: paragraphStyle range: NSMakeRange( 0, [str length] ) ];
            [[m_textView textStorage] replaceCharactersInRange: NSMakeRange( 0, [str length] ) withAttributedString: attrString];
            m_textView.typingAttributes = [NSDictionary dictionaryWithObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
            [paragraphStyle release];
            [attrString autorelease];
            [m_textView display];
            style.SetLeftIndent( false );
        }
        [m_textView setTypingAttributes:attrs];

Like I said, there is only one possibility either color scheme or left indentation but not both.

In the caller, I set the the for the paragraph first and then I call this piece of code applying an appropriately set styles.

I'm pretty new to Cocoa programming and want to learn.
Thank you.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.