Professional Web Applications Themes

NSMutableAttributedString in an NSTextView - Mac Programming

In <210720032130382351%sgketchamcomcast.net> Steve Ketcham wrote: > For the past few days, I've been driving myself crazy trying to apply > paragraph formatting to a cell in an NSTextView. Do you mean an NSTableView? > In -tableView:objectValueForTableColumn:row, I set up the > NSMutableAttributedString to contain the needed text, then apply the > formatting to the string with addAttribute:value:range. I also add > attributes to the string to change the foreground color and font size. > > My NSTextView is a one-month calendar. Do you mean an NSTableView? > When I display, everything > looks great. But if I move to another ...

  1. #1

    Default Re: NSMutableAttributedString in an NSTextView

    In <210720032130382351%sgketchamcomcast.net> Steve Ketcham wrote:
    > For the past few days, I've been driving myself crazy trying to apply
    > paragraph formatting to a cell in an NSTextView.
    Do you mean an NSTableView?
    > In -tableView:objectValueForTableColumn:row, I set up the
    > NSMutableAttributedString to contain the needed text, then apply the
    > formatting to the string with addAttribute:value:range. I also add
    > attributes to the string to change the foreground color and font size.
    >
    > My NSTextView is a one-month calendar.
    Do you mean an NSTableView?
    > When I display, everything
    > looks great. But if I move to another month, or even redisplay the
    > same month, most of the cells are not displayed. And I can't figure
    > out why.
    It sounds to me like the problem is that the value you are returning no
    longer exists. You have probably failed to retain or copy something, so
    you're passing nil or garbage by mistake. That's just a guess, but it's
    a frequent cause of this sort of thing. It works the first time because
    we're still in the initial call stack, but then the call stack ends, the
    object is released, and so the next time, it's not there. m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  2. #2

    Default Re: NSMutableAttributedString in an NSTableView

    In article <20030721203124248-0700news.la.sbcglobal.net>, matt neuburg
    <matttidbits.com> wrote:
    > In <210720032130382351%sgketchamcomcast.net> Steve Ketcham wrote:
    > > For the past few days, I've been driving myself crazy trying to apply
    > > paragraph formatting to a cell in an NSTextView.
    >
    > Do you mean an NSTableView?
    Yes. Sorry. I proofread this several times, and still missed this.
    >
    > > In -tableView:objectValueForTableColumn:row, I set up the
    > > NSMutableAttributedString to contain the needed text, then apply the
    > > formatting to the string with addAttribute:value:range. I also add
    > > attributes to the string to change the foreground color and font size.
    > >
    > > My NSTextView is a one-month calendar.
    >
    > Do you mean an NSTableView?
    Yes.
    >
    > > When I display, everything
    > > looks great. But if I move to another month, or even redisplay the
    > > same month, most of the cells are not displayed. And I can't figure
    > > out why.
    >
    > It sounds to me like the problem is that the value you are returning no
    > longer exists. You have probably failed to retain or copy something, so
    > you're passing nil or garbage by mistake. That's just a guess, but it's
    > a frequent cause of this sort of thing. It works the first time because
    > we're still in the initial call stack, but then the call stack ends, the
    > object is released, and so the next time, it's not there. m.
    That's what I thought too, because that's a pretty common source of
    problems like this. So I added "retain" to the code in -init, and when
    that didn't help, I moved the creation and setup of the
    NSMutableParagraphStyle into the
    tableView:objectValueForTableColumn:row. Neither change makes a
    difference to the behavior described.

    Do you have any other ideas?

    Thanks,
    Steve
    Steve Ketcham Guest

  3. #3

    Default Re: NSMutableAttributedString in an NSTableView

    In <230720031732509909%sgketchamcomcast.net> Steve Ketcham wrote:
    > That's what I thought too, because that's a pretty common source of
    > problems like this. So I added "retain" to the code in -init, and
    > when that didn't help, I moved the creation and setup of the
    > NSMutableParagraphStyle into the tableView:objectValueForTableColumn:
    > row. Neither change makes a difference to the behavior described.
    >
    > Do you have any other ideas?
    Not without seeing more of your code than you have so far been willing
    to divulge. m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  4. #4

    Default Re: NSMutableAttributedString in an NSTableView

    In article <20030724072233782-0700news.la.sbcglobal.net>, matt neuburg
    <matttidbits.com> wrote:

    > > Do you have any other ideas?
    >
    > Not without seeing more of your code than you have so far been willing
    > to divulge. m.
    Not so much unwilling as unwieldy...

    Let me see if I can put together a test case that demonstrates the
    problem. (That's probably a good exercise anyway.) It may take a
    couple of days.

    Thanks,
    Steve
    Steve Ketcham Guest

  5. #5

    Default Re: NSMutableAttributedString in an NSTableView

    In article <20030724072233782-0700news.la.sbcglobal.net>, matt neuburg
    <matttidbits.com> wrote:
    > In <230720031732509909%sgketchamcomcast.net> Steve Ketcham wrote:
    > > That's what I thought too, because that's a pretty common source of
    > > problems like this. So I added "retain" to the code in -init, and
    > > when that didn't help, I moved the creation and setup of the
    > > NSMutableParagraphStyle into the tableView:objectValueForTableColumn:
    > > row. Neither change makes a difference to the behavior described.
    > >
    > > Do you have any other ideas?
    >
    > Not without seeing more of your code than you have so far been willing
    > to divulge. m.
    Matt (and others)

    I've put together a test case that shows the problem without all the
    other baggage of the original application. Download
    [url]http://sgketcham.home.comcast.net/TestCase.sit.bin[/url] to see a
    demonstration of the problem.

    There are two build versions of the program. One's called "NSString
    Version" and behaves as I'd expect it to. The other is called
    "NSAttributedString Version" and shows only the first month correctly.

    You must do a "clean target" between building the versions, so that the
    application is recompiled.

    There's a ReadMe that describes the process, too.

    Feel free to contact me directly, or to keep this in the group if you
    think it's of general interest.

    Thanks again,
    Steve
    Steve Ketcham Guest

  6. #6

    Default Re: NSMutableAttributedString in an NSTableView

    In <240720031715121173%sgketchamcomcast.net> Steve Ketcham wrote:
    > I've put together a test case that shows the problem without all the
    > other baggage of the original application.
    Very nicely done; thank you for putting this together so well. You were
    not doing anything wrong; your memory management was a little dubious
    but it wasn't the cause of the problem. In fact, your code was working;
    but the attributed strings weren't being displayed.

    Here's the solution. After setDataSource, also setDelegate so that
    TCCalendar is the table's delegate. Throw away calendarCellForRow:column:
    and the call to it; return an ordinary string from the data source. Now
    implement this delegate method:

    - (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell
    forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {
    NSString* s = [self tableView: aTableView
    objectValueForTableColumn: aTableColumn
    row: rowIndex];
    if (!s) s = "";
    NSCell* c = aCell;
    [c setStringValue: s];
    [c setFont: [NSFont userFontOfSize:14]];
    [c setAlignment: NSRightTextAlignment];
    }

    The problem, I think, is that this is a cell, not a text view, so you
    need to use cell-type formatting, not paragraph-type formatting. This
    could be a bug, but I have no way of knowing what the intended behavior
    of attributed strings is, so I have to be content with giving you a
    solution that works. Actually, though, you could just skip this step and
    set these attributes in IB (unless you will ultimately need them to be
    different for particular cells). m.


    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  7. #7

    Default Re: NSMutableAttributedString in an NSTableView

    In <240720032253379497%sgketchamcomcast.net> Steve Ketcham wrote:
    > In article <20030724170212167-0700news.la.sbcglobal.net>, matt
    > neuburg <matttidbits.com> wrote:
    >
    > Thanks, I'll do this. I'm not sure if it will do what I need,
    > because my original plan was to put the date in the top right corner,
    > and a holiday in the bottom, in a smaller font and perhaps a different
    > color. In other words, I was planning two paragraphs, with different
    > attributes. But I'll read about cell formatting, and see what I might
    > be able to do.
    >>
    >> The problem, I think, is that this is a cell, not a text view, so you
    >> need to use cell-type formatting, not paragraph-type formatting. This
    >> could be a bug, but I have no way of knowing what the intended
    >> behavior of attributed strings is, so I have to be content with
    >> giving you a solution that works.
    >
    > Now that I have a test case that demonstrates the problem, is it worth
    > submitting it to Apple for their opinion? If so, what do I do?
    Beats me. I really don't understand what's going on here. But it was
    clear to me that the cause of the problem was the NSAttributedString
    with the paragraph styling. Maybe there's another way to make this work (
    why, after all, does it work the first time but not when the table is
    refreshed?).

    I do have one positive suggestion, though; I wonder whether it might not
    be a whole lot easier if you were using a matrix instead of a table. It
    seems to me that what you're doing here is not at all suitable to what
    an NSTableView is about, and that it is exactly what an NSMatrix is for.
    m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  8. #8

    Default Re: NSMutableAttributedString in an NSTableView

    In article <20030724201949207-0700news.la.sbcglobal.net>, matt neuburg
    <matttidbits.com> wrote:
    > In <240720032253379497%sgketchamcomcast.net> Steve Ketcham wrote:
    > > In article <20030724170212167-0700news.la.sbcglobal.net>, matt
    > > neuburg <matttidbits.com> wrote:
    >
    > I do have one positive suggestion, though; I wonder whether it might not
    > be a whole lot easier if you were using a matrix instead of a table. It
    > seems to me that what you're doing here is not at all suitable to what
    > an NSTableView is about, and that it is exactly what an NSMatrix is for.
    Geez! Another class to read about...

    I'll look at it, because you're probably right. I chose NSTableView
    because I finally have that one figured out.

    In the meantime, I was reading about NSCell, and discovered
    setAttributedStringValue:. It appears to do what I want, and the class
    doentation even has some sample code. (It must be an accident.)

    Steve
    Steve Ketcham Guest

  9. #9

    Default Re: NSMutableAttributedString in an NSTableView

    In article <240720032345437036%sgketchamcomcast.net>, Steve Ketcham
    <sgketchamcomcast.net> wrote:
    >
    > In the meantime, I was reading about NSCell, and discovered
    > setAttributedStringValue:. It appears to do what I want, and the class
    > doentation even has some sample code. (It must be an accident.)
    >
    I've just combined this with your delegate code, and it works like a
    champion. Therefore, my code probably isn't a bug, it's incorrect and
    the delegate with setAttributedStringValue: should be used.

    Thanks again,
    Steve
    Steve Ketcham Guest

  10. #10

    Default Re: NSMutableAttributedString in an NSTableView

    In <250720030032204868%sgketchamcomcast.net> Steve Ketcham wrote:
    > In article <240720032345437036%sgketchamcomcast.net>, Steve Ketcham
    > <sgketchamcomcast.net> wrote:
    >
    >>
    >> In the meantime, I was reading about NSCell, and discovered
    >> setAttributedStringValue:. It appears to do what I want, and the
    >> class doentation even has some sample code. (It must be an
    >> accident.)
    > I've just combined this with your delegate code, and it works like a
    > champion. Therefore, my code probably isn't a bug, it's incorrect and
    > the delegate with setAttributedStringValue: should be used.
    That's great but I *still* think you should be using an NSMatrix. The
    fact that you've got NSTableView "figured out" and that NSMatrix is
    "another class to read about" is not an excuse. Especially when we
    consider that you don't scroll and you never intend to, and that you are
    not fetching values from a real data source, your use of NSTableView is
    simply adding unnecessary complications. The fact is that since you are
    dealing with the cell directly at this point you are essentially
    treating the NSTableView as if it *were* a matrix (a matrix consists of
    cells). Thus your code would actually get *simpler* if you used a matrix.
    The art of using a framework is using the right tool for the job. m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  11. #11

    Default Re: NSMutableAttributedString in an NSTableView

    In article <20030725103418048-0700news.la.sbcglobal.net>, matt neuburg
    <matttidbits.com> wrote:
    >
    > That's great but I *still* think you should be using an NSMatrix. The
    > fact that you've got NSTableView "figured out" and that NSMatrix is
    > "another class to read about" is not an excuse. Especially when we
    > consider that you don't scroll and you never intend to, and that you are
    > not fetching values from a real data source, your use of NSTableView is
    > simply adding unnecessary complications. The fact is that since you are
    > dealing with the cell directly at this point you are essentially
    > treating the NSTableView as if it *were* a matrix (a matrix consists of
    > cells). Thus your code would actually get *simpler* if you used a matrix.
    > The art of using a framework is using the right tool for the job. m.
    Actually, I agree, now that I've seen the difference, and I've started
    to read about NSMatrix. (The "Oh geez" comment was meant either as a
    joke, or a comment on the framework in general. I'm not sure which.)

    Tell me: is there a way in IB to create an NSMatrix of NSCells (or of
    my own subclass of NSCell)? All the examples I've found are matrices
    of buttons, or other controls.

    S
    Steve Ketcham Guest

  12. #12

    Default Re: NSMutableAttributedString in an NSTableView

    In <250720031604333409%sgketchamcomcast.net> Steve Ketcham wrote:
    > In article <20030725103418048-0700news.la.sbcglobal.net>, matt
    > neuburg <matttidbits.com> wrote:
    >>
    > Tell me: is there a way in IB to create an NSMatrix of NSCells (or of
    > my own subclass of NSCell)?
    Not that I know of.
    > All the examples I've found are matrices
    > of buttons, or other controls.
    Actually there is no such thing as a matrix of controls. When, for
    example, you use IB to make a matrix of NSButtons, you're really making
    a matrix of NSButtonCells; in other words, that's merely IB giving you a
    shorthand way of making a matrix of the same NSCell that forms the
    displayed part of an NSButton. If you really must use IB, make a matrix
    of anything at all and then in code substitute a different prototype. m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

Similar Threads

  1. delegate or subclass NSTextView?
    By None in forum Mac Programming
    Replies: 3
    Last Post: October 1st, 08:58 AM
  2. NSTextView problems plus a few others...
    By Per Bull Holmen in forum Mac Programming
    Replies: 6
    Last Post: September 18th, 12:01 AM
  3. NSScrollView for NSTextView scrolls then snaps back
    By Harry Mahoney in forum Mac Programming
    Replies: 0
    Last Post: July 16th, 09:19 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139