• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSAttributedString & alert panels?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSAttributedString & alert panels?


  • Subject: Re: NSAttributedString & alert panels?
  • From: Andy Lee <email@hidden>
  • Date: Tue, 24 Jan 2006 11:51:30 -0500

On Jan 24, 2006, at 10:04 AM, Camillo Lugaresi wrote:
On 23 Jan 2006, at 11:53, Jonathan del Strother wrote:

Is there any way to get NSRunAlertPanel or NSAlert to use attributed strings?

I'd like to place a link in the text - do I have to create a custom window just for this? Or any tips what I'd need to do to subclass the NSAlert class to do this for me?


Anyone?


I don't think you need a subclass. Use the window method to get the window of the NSAlert, then do whatever you want with it. For example, you could look for a text field inside the window whose text is the unattributed string you passed to setInformativeText, and if you find it customize it as needed. Of course, the layout of alerts could change in the future, so be careful and make sure you handle failures gracefully.

Out of curiosity I just tried this, and the string is still displayed unattributed. Maybe someone can find the flaw in my code, or suggest a workaround.


To find the text field to tweak, I wrote a method called - findTextFieldWithString:inView:, which looks like this:


// quick and dirty -- yeah, could be a category of NSView, and could
// parametrize the "test" as a SEL or function pointer
- (NSTextField *)findTextFieldWithString:(NSString *)s inView:(NSView *)view
{
if (view == nil)
{
return nil;
}


    // Apply the test to the given view.
    if ([view isKindOfClass:[NSTextField class]]
        && [[(NSTextField *)view stringValue] isEqualToString:s])
    {
        return (NSTextField *)view;
    }

    // The given view didn't pass the test, so search its subviews.
    NSArray *subviews = [view subviews];
    int n = [subviews count];
    int i;
    for (i = 0; i < n; i++)
    {
        NSTextField *v =
            [self
                findTextFieldWithString:s
                inView:[subviews objectAtIndex:i]];
        if (v) return v;
    }

// If we got this far, we failed to find a view that passes the test.
return nil;
}



To try tweaking an NSAlert, I added a file called Scratch.rtf to my project and added this -awakeFromNib method to my app delegate:



- (void)awakeFromNib
{
// Load attributed string from file.
NSString *rtfPath =
[[NSBundle mainBundle] pathForResource:@"Scratch" ofType:@"rtf"];
NSLog(@"rtfPath=[%@]", rtfPath);
NSAttributedString *attrString =
[[[NSAttributedString alloc]
initWithPath:rtfPath
documentAttributes:(NSDictionary **)NULL]
autorelease];
NSLog(@"attrString=[%@]", attrString);


// Try to put the attributed string into an alert. Note the call to
// -setAllowsEditingTextAttributes:, which allows the text field to
// show an attributed string.
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setInformativeText:@"abcd"];
NSTextField *textField =
[self
findTextFieldWithString:@"abcd"
inView:[[alert window] contentView]];
NSLog(@"textField=[%@]", textField);
[textField setAllowsEditingTextAttributes:YES];
[textField setAttributedStringValue:attrString];
[alert runModal]; // <-- the string still comes out as plain text
}



I can plug the attributed into an NSTextField in a regular window, and it works okay. It looks like NSAlert somehow coerces the string to be unattributed.


Note that even if the hack had worked, there could be potential issues with how NSAlert autosizes the window, since the size could in theory be significantly different for the attributed and plain versions of the same string. I don't know how NSAlert computes the window size, so this might not be an issue, and the few pixels might not matter anyway in most cases.

I'd file an enhancement request for this. It seems reasonable to want to put rich text, especially links, into an alert.

--Andy

P.S. I only now discovered that if I set my mail message to rich text, the syntax highlighting from Xcode gets pasted in.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSAttributedString & alert panels?
      • From: Jonathan del Strother <email@hidden>
References: 
 >NSAttributedString & alert panels? (From: Jonathan del Strother <email@hidden>)
 >Re: NSAttributedString & alert panels? (From: Jonathan del Strother <email@hidden>)
 >Re: NSAttributedString & alert panels? (From: Camillo Lugaresi <email@hidden>)

  • Prev by Date: Re: invertrect, any cocoa versions suggested?
  • Next by Date: Re: what is [textStorage paragraphs]???
  • Previous by thread: Re: NSAttributedString & alert panels?
  • Next by thread: Re: NSAttributedString & alert panels?
  • Index(es):
    • Date
    • Thread