NSTextView not displaying attributed strings as bold

  • Hello,

    In my first decent-sized project in OS X, I have the following basic
    design:

    parse input text file,
    for each element in the text file, apply attributes in an attributed
    string,
    append this attributed string to a single attributed string,
    display the large attributed string in an NSTextView.

    I believe the first three portions are working correctly; at least, the
    debugger gives me heartening output for these individual
    NSMutableAttributedStrings and the final string to output. For example,
    'po output', where output is an NSMutableAttributedString, gives:

    .Dd April 10, 2001.Os.Dt GROFF_MDOC 7{}NAME
    {
        NSFont = "CGS Helvetica-Bold 12.00 pt. P [] (0x021ebda0)
    fobj=0x01c55400, spc=3.33";
    }.Nm groff_mdoc{}SYNOPSIS
    {
        NSFont = "CGS Helvetica-Bold 12.00 pt. P [] (0x021ebda0)
    fobj=0x01c55400, spc=3.33";
    }
    ...

    Hopefully people familiar with mdoc macros will understand what I am
    attempting to do here. Above, output was generated by, among other things,
      this function:

    - (void)_sectionHeader:(NSArray *)args
    {
        NSMutableAttributedString* out;
        NSString* argString;
        NSRange myRange;
    // argString is set here
        ...
        out = [[NSMutableAttributedString alloc] initWithString:argString];

        myRange.location = 0;
        myRange.length = [out length];
        [out addAttribute:NSFontAttributeName value:propFont range:myRange];
        [out applyFontTraits:NSBoldFontMask range:myRange];

        [theOutput appendAttributedString:out];
    }

    ... where propFont is an instance variable currently set to userFontOfSize:
    12

    This function is meant to apply boldfaced type to the arguments of an mdoc
    macro (in this case .Sh). Applied to (.Sh, NAME), it returns (gdb output
    of attrib string):
    NAME
    {
        NSFont = "CGS Helvetica-Bold 12.00 pt. P [] (0x01c4eb00)
    fobj=0x01c56c90, spc=3.33";
    }

    I am assuming that this is what is expected. All seems to go well until I
    try to display this text in an NSTextView. If I use the following code:

        [theTextView setEditable:YES];
        [theTextView insertText:output];
        [theTextView scrollRangeToVisible: NSMakeRange(0,0)];
        [theTextView setEditable:NO];

    where output is the concatenation of all the NSMutableAttributedStrings
    generated by methods like _sectionHeader, the text appears in the window
    but is not bold.

    My best guesses are either that insertText is not the method to use here,
    and it loses the attributes (though from the documentation it seems not),
    or there is an issue in the way I am generating the attributed strings
    which are supposedly bold. A third option which I have yet to explore is
    that Helvetica doesn't have a bold version, but that seems rather unlikely.
      As I said above, I'm quite new to programming in Objective-C and in Cocoa,
      so it's likely that I've completely misunderstood many things about what
    I'm doing.

    Thanks!

    -- Steve
  • On Monday, June 24, 2002, at 11:59  AM, Steven Huwig wrote:

    > If I use the following code:
    >
    > [theTextView setEditable:YES];
    > [theTextView insertText:output];
    > [theTextView scrollRangeToVisible: NSMakeRange(0,0)];
    > [theTextView setEditable:NO];
    >
    > where output is the concatenation of all the NSMutableAttributedStrings
    > generated by methods like _sectionHeader, the text appears in the
    > window but is not bold.

    Read the docs on insertText:.  In particular, the line "The inserted
    text is assigned the current typing attributes."  Use this code instead:

    [[theTextView textStorage] appendAttributedString:output];

    Hope this helps.

    -Peter
  • On Wednesday, June 26, 2002, at 03:24 AM, Peter Ammon wrote:

    On Monday, June 24, 2002, at 11:59  AM, Steven Huwig wrote:

      If I use the following code:

        [theTextView setEditable:YES];
        [theTextView insertText:output];
        [theTextView scrollRangeToVisible: NSMakeRange(0,0)];
        [theTextView setEditable:NO];

    where output is the concatenation of all the NSMutableAttributedStrings
    generated by methods like _sectionHeader, the text appears in the window
    but is not bold.

    Read the docs on insertText:.  In particular, the line "The inserted text
    is assigned the current typing attributes."  Use this code instead:

    [[theTextView textStorage] appendAttributedString:output];

    Hope this helps.

    -Peter

    Peter,

    Thanks for replying! I had seen that note, and came to the same conclusion
    that you did, but unfortunately when I try it the program hangs. The
    window draws, but no text is drawn and there is a perpetual spinning beach
    ball. I've looked in the docs for information about this, but I can't seem
    to quite grasp the text system and why the code you sent doesn't work.

    Thanks,
    Steve
  • On Wednesday, June 26, 2002, at 03:24 AM, Peter Ammon wrote:

    On Monday, June 24, 2002, at 11:59  AM, Steven Huwig wrote:

      If I use the following code:

        [theTextView setEditable:YES];
        [theTextView insertText:output];
        [theTextView scrollRangeToVisible: NSMakeRange(0,0)];
        [theTextView setEditable:NO];

    where output is the concatenation of all the NSMutableAttributedStrings
    generated by methods like _sectionHeader, the text appears in the window
    but is not bold.

    Read the docs on insertText:.  In particular, the line "The inserted text
    is assigned the current typing attributes."  Use this code instead:

    [[theTextView textStorage] appendAttributedString:output];

    Hope this helps.

    -Peter

    Peter,

    Thanks for replying! I had seen that note, and came to the same conclusion
    that you did, but unfortunately when I try it the program hangs. The
    window draws, but no text is drawn and there is a perpetual spinning beach
    ball. I've looked in the docs for information about this, but I can't seem
    to quite grasp the text system and why the code you sent doesn't work.

    Thanks,
    Steve
  • On Wednesday, June 26, 2002, at 03:24 AM, Peter Ammon wrote:

    On Monday, June 24, 2002, at 11:59  AM, Steven Huwig wrote:

      If I use the following code:

        [theTextView setEditable:YES];
        [theTextView insertText:output];
        [theTextView scrollRangeToVisible: NSMakeRange(0,0)];
        [theTextView setEditable:NO];

    where output is the concatenation of all the NSMutableAttributedStrings
    generated by methods like _sectionHeader, the text appears in the window
    but is not bold.

    Read the docs on insertText:.  In particular, the line "The inserted text
    is assigned the current typing attributes."  Use this code instead:

    [[theTextView textStorage] appendAttributedString:output];

    Hope this helps.

    -Peter

    Peter,

    Thanks for replying! I had seen that note, and came to the same conclusion
    that you did, but unfortunately when I try it the program hangs. The
    window draws, but no text is drawn and there is a perpetual spinning beach
    ball. I've looked in the docs for information about this, but I can't seem
    to quite grasp the text system and why the code you sent doesn't work.

    Thanks,
    Steve
  • >
    > On Wednesday, June 26, 2002, at 03:24 AM, Peter Ammon wrote:
    >
    > On Monday, June 24, 2002, at 11:59  AM, Steven Huwig wrote:
    >
    > If I use the following code:
    >
    > [theTextView setEditable:YES];
    > [theTextView insertText:output];
    > [theTextView scrollRangeToVisible: NSMakeRange(0,0)];
    > [theTextView setEditable:NO];
    >
    > where output is the concatenation of all the NSMutableAttributedStrings
    > generated by methods like _sectionHeader, the text appears in the window
    > but is not bold.
    >
    > Read the docs on insertText:.  In particular, the line "The inserted text
    > is assigned the current typing attributes."  Use this code instead:
    >
    > [[theTextView textStorage] appendAttributedString:output];
    >
    > Hope this helps.
    >
    > -Peter
    >
    >
    >
    > Peter,
    >
    > Thanks for replying! I had seen that note, and came to the same conclusion
    > that you did, but unfortunately when I try it the program hangs. The
    > window draws, but no text is drawn and there is a perpetual spinning beach
    > ball. I've looked in the docs for information about this, but I can't seem
    > to quite grasp the text system and why the code you sent doesn't work.
    >
    > Thanks,
    > Steve

    First I'd like to apologize for the multiple posting -- the smtp server I
    use seemed to be having issues. Second I'd like to apologize for posting
    in rich text (thought I had reset that preference).

    Now, on to my issues. First, the NSAttributedString I am making by
    repeatedly using -appendAttributedString does show up (without attributes)
      when using -insertText; unfortunately, it does not when using
    -appendAttributedString or -setAttributedString on my NSTextView's
    textStorage. However, other strings do in fact display using Peter's
    suggestion -- just not the one I want to display. So there must be
    something about this string which is not kosher with NSTextStorage, near
    as I can figure, and that something is ignored by NSTextView's -insertText.

    Does anyone have any insight on this? The documentation I have (from the
    April Developer Tools) is very spotty, and looking back on it, the phrase
    "The inserted text is assigned the current typing attributes" is hard to
    get at; the link at the beginning of NSTextView.html takes you to the
    documentation for NSTextInput's -insertText method, which is less
    informative. I have a hunch that there's probably something similar
    keeping me from spotting my current problem.

    -- Steve
  • It could be a small font size issue, bold sometime does not look bold in
    some type faces when the font size is too small.
    Try increasing the font size and creating a bold and plain text
    attribute string and see if they are different.

    Mark,

    The Photographer and Author of:
    * Images of the World CD-ROM, PhotoMover Upload tool,

    * MSM IP Setup is a MacOS X network setup tool (useful for ISP's, no
    programming needed to configure, public domain source):
    http://www.ImageMontage.com/MacosX-IPSetup/