NSAttributedString in a shaped box ()

Discussion in 'Mac Programming' started by superscape, Jun 16, 2015.

  1. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #1
    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.
     

    Attached Files:

  2. superscape thread starter macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
  3. Plumhead macrumors newbie

    Joined:
    Feb 3, 2012
    Location:
    Uk
    #3
    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!)
     
  4. superscape thread starter macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Most useful, thanks for that from Sunny Hull.
     

Share This Page