Cocoa Text System Sample

Cocoa is pretty amazing. Here’s a quick sample that does a simple text layout (with styles!) inside a NSTextView. This is an AwakeFromNib method that was instantiated inside Interface Builder and connected to Main Window’s Content area.

-(void) awakeFromNib{
  // create the Text
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]
    initWithString:@"Lorem ipsum dolor sit amet, consectetuer adipiscing elit."];
// the text property that we want to modify, in this case paragraph style.
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setFirstLineHeadIndent:65.0];
// apply the new style to the whole string.
[text addAttribute:NSParagraphStyleAttributeName	value:paragraphStyle range:NSMakeRange(0,[text length])];
// contentView is the Window's content area, configured in IB.	NSRect frame = [contentView frame];
// initialize TextView control.
NSTextView *textView = [[NSTextView alloc] initWithFrame:frame];
// ... stick the text in.
[textView insertText:text];
[textView setEditable:NO];
[textView setSelectable:NO];
// attach the TextView to the window.
[contentView addSubview:textView];}

More information @ Text System Overview.

Post a Comment

Your email is never published nor shared. Required fields are marked *