6

I am looking for a simple way to use NSAttributedString with a very simple message box similar to:

NSString *new_item = [NSString stringWithFormat:@"<span style=\"font-family: Helvetica Neue; font-size: 12.0\">%@</span>", @"MOTD HTML String downloaded from website"];
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[new_item dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MOTD"
                                                   message:attrStr
                                                  delegate:nil
                                         cancelButtonTitle:@"OK"
                                         otherButtonTitles:nil];
[alert show];

My above code takes a HTML formatted string that has been downloaded from a server, makes sure the text size will fit the screen properly, then tries to send the NSAttributedString to the UIAlertView. But UIAlertView does not like this. what would be the simplest way around this problem?(Non HTML formatted MOTD is not an option)

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
theshadow124
  • 661
  • 3
  • 8
  • 29
  • Neither `UIAlertView` nor `UIAlertController` work with attributed strings. Either find a 3rd party replacement that does or write your own. – rmaddy May 09 '15 at 04:55
  • any suggestion on what 3rd party lib to use? I was looking on cocoa controls but all of them seem way more complicated then what I'm looking for – theshadow124 May 09 '15 at 05:03
  • Guess the best way would be to create my own, good example here: http://stackoverflow.com/questions/19118919/custom-view-which-looks-like-uialertview/19119307#19119307 – theshadow124 May 09 '15 at 05:10

4 Answers4

15

Add your attributed string to label and add it as assessoryView to alert

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Test Attributed String" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc]initWithString:@"Hello red"];
[attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,5)];
lbl.attributedText = attributedStr;
lbl.textAlignment = NSTextAlignmentCenter;
[alert setValue:lbl forKey:@"accessoryView"];
[alert show];

Now a days UIAlertView is deprecated. You can use UIAlertController.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
5

I got an idea from the @AshishKakkad answer only (+1). However, its UI isn't properly visible. So I'm showing you a way to format your message with attributedString.

Here's how I'm doing it:

NSMutableString *message = [NSMutableString string];
NSString *title = @"Message Heading";
[message appendString:title];
[message appendString:@"\n\n• Topic 1"];
[message appendString:@"\n• Topic 2"];
[message appendString:@"\n• Topic 3\n"]; //Important, I have added '\n' at last to have some extra space at bottom.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"" delegate:self cancelButtonTitle:@"CancelButton" otherButtonTitles:@"OtherButton", nil];
//need to set a label as `accessoryView` of an alert.
[alert setValue:[self getLabelWithMessage:message withTitle:title] forKey:@"accessoryView"];
[alert show];

- (UILabel *)getLabelWithMessage:(NSMutableString *)message withTitle:(NSString *)title {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setAlignment:NSTextAlignmentLeft];
    paragraphStyle.paragraphSpacing = 2.0f;
    paragraphStyle.headIndent = 20.0;
    paragraphStyle.firstLineHeadIndent = 20.0;
    paragraphStyle.tailIndent = -20.0;

    NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:message];
    [attribString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [message length])];
    [attribString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12.f] range:NSMakeRange(0, [message length])];
    [attribString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:12.f] range:NSMakeRange(0, [title length])];

    UILabel *label = [[UILabel alloc] init];
    [label setAttributedText:attribString];
    [label setNumberOfLines:0];
    [label sizeToFit];
    return label;
}

enter image description here

Hemang
  • 26,840
  • 19
  • 119
  • 186
2

don't forget to set:

lbl.numberOfLines = 0;

when you have multiple lines

tuvok
  • 679
  • 5
  • 16
0

As an alternative to private API you could use https://github.com/AntonPoltoratskyi/NativeUI

pod 'NativeUI/Alert', '~> 1.0'

It looks exactly like native alert, but configurable enough.

Please look https://stackoverflow.com/a/61047809/6726766 for more details.

A. Poltoratskyi
  • 410
  • 6
  • 13