Mac NSAttributedString in a shaped box ()

superscape

macrumors 6502a
Original poster
Feb 12, 2008
930
218
East Riding of Yorkshire, UK
Hi,

Just after a very vague pointer here.

I've got an NSView and I'd like to add an NSAttributedString to it in a *shaped* text box (that's to say, a box defined by a bezier path). See attached image to see what I mean. I guess I need an NSTextView that isn't rectangular but I don't see how to do that, or if it's possible at all.

Any suggestions as to how I might go about it?

Thanks
r.
 

Attachments

Plumhead

macrumors newbie
Feb 3, 2012
6
0
Uk
Hi,

Just after a very vague pointer here.

I've got an NSView and I'd like to add an NSAttributedString to it in a *shaped* text box (that's to say, a box defined by a bezier path). See attached image to see what I mean. I guess I need an NSTextView that isn't rectangular but I don't see how to do that, or if it's possible at all.

Any suggestions as to how I might go about it?

Thanks
r.
By coincidence I was playing with shaped text containers yesterday. Whilst not following a curved path the code below should give you a good clue how to achieve the effect you're looking for. Use this to replace the text container (you were right) for your view and see the output.

Code:
import Cocoa

class PathTextContainer : NSTextContainer {
  
    override func lineFragmentRectForProposedRect(proposedRect: NSRect, sweepDirection: NSLineSweepDirection, movementDirection: NSLineMovementDirection, remainingRect: NSRectPointer) -> NSRect {
        let containerSize = self.containerSize
        let middle = containerSize.height / 2
        remainingRect.initialize(NSRect.zeroRect)
      
        if proposedRect.origin.y > middle {
            let offset = (proposedRect.origin.y - middle) / middle
            let offsetX = containerSize.width * offset
            let result = NSRect(x: offsetX, y: proposedRect.origin.y, width: containerSize.width - offsetX, height: proposedRect.size.height)
            return result
        }
        else {
            return NSRect(x: proposedRect.origin.x, y: proposedRect.origin.y, width: containerSize.width, height: proposedRect.size.height)
        }
    }
  
}
This will give a very simple contrived path which gradually indents from the midway point in the view container as shown in the screenshot below.

Screen Shot 2015-06-17 at 08.13.11.png


Note, whilst not used here the setting of remainingRect to something other than RectZero can achieve some neat effects.


Hope this helps (a fellow Yorkshireman!)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.