Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does NSAttributedString follow the MVC paradigm?

Can anyone explain to me how NSAttributedString correctly follows the MVC paradigm? I know it doesn't inherit from NSString, but it still is a string, so I would say that's part of my model. However, setting UI attributes such as underline, font, shadow, etc. clearly are part of the View when talking MVC, so I'm not really sure how this follows the rules.

like image 554
Scott Berrevoets Avatar asked Dec 06 '25 17:12

Scott Berrevoets


2 Answers

NSAttributedString is a model class.

It just has the representation of the attributed string.

Just because it stores the attributes, that doesn't make it responsible for the display of the attribute.

Imagine you had a custom class for shapes. If you had a square shape and stored it's colour, it would still be a model object, because it's up to the presenting view to draw the square and fill it with the colour, the model object is just a place to store the attributes.

like image 196
Abizern Avatar answered Dec 08 '25 06:12

Abizern


NSAttributedString provides infrastructure and is the superclass for Cocoa's styled text model class, NSTextStorage. An NSTextStorage is just an NSAttributedString that can talk to layout managers and does some bookkeeping/convenience methods with regard to editing.

The controller duties are handled by NSLayoutManager. Alot of this has to do with turning unicode strings into glyphs, styling them and doing the various geometry calculations. It does all of this with regard to NSTextContainer objects, which (in the base class's case) are just fancy NSSizes the layout manager can use to calculate where a glyph should be stroked, where line breaks should happen, etc.

Cocoa's view class for styled text is NSText and its subclasses, working with the rulers, font and color panels. NSTextView is an NSText subclass, and NSCells share an NSText object to edit their content. NSText is an NSResponder and it accepts text editing messages for its textStorage and layoutManager, if present.

like image 36
iluvcapra Avatar answered Dec 08 '25 06:12

iluvcapra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!