CocoaDev

Edit AllPages

An NSTextContainer defines a region where text is laid out. An NSLayoutManager uses NSTextContainer (s) to determine where to break lines, lay out portions of text, and so on. NSTextContainer defines rectangular regions, but you can create subclasses that define regions of other shapes, such as circular regions, regions with holes in them, or regions that flow alongside graphics.

http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextContainer_Class/index.html#//apple_ref/doc/uid/TP40004132


I think of NSTextContainer as a sort of ‘scratch pad’ for NSLayoutManager to work in before drawing to a view.


Has any one worked on irregular shaped text container??? —- The following is a sample that performs text layout and editing within a text container defined by an arbitrary closed convex Bezier path. The sample is deliberately kept simple and uses a brute force technique. There are suggestions for improvements in the code. For a more general solution, mix this sample with arbitrary path filling logic. The seminal Graphics GEMS series will aid with path filling. The Opensource libArt project demonstrates an efficient way to handle concave paths, paths with voids (doughnuts), and vastly more efficient convex paths. libArt conveniently “raterizes” to horizontal rectangles which are exacly what is needed for this sample.

@interface MyBezierTextContainer : NSTextContainer { id pathSource; }

@end

#import “MyBezierTextContainer.h”

@interface NSObject (MyBezierTextContainerPathSource)

@end

@implementation MyBezierTextContainer

@end

To test the custom text container, create a standard Cocoa multi-document application and add the following code to the document class: (You will want to connect the textView outlet to a text view within the document nib. MyDocument.h

@class MyBezierTextView;

@interface MyDocument : NSDocument { NSBezierPath *bezierPath; IBOutlet NSTextView *textView; }

@end

@end

If you want to be able to see the path in which the text flows, use the following simple subclass of NSTextView instead of the base NSTextView class: MyBezierTextView.h

@interface MyBezierTextView : NSTextView { id pathSource; }

@end

MyBezierTextView.m

#import “MyBezierTextView.h”

@interface NSObject (MyBezierTextContainerPathSource)

@end

@implementation MyBezierTextView

@end