Simple UIAlertView with NSAttributedString (s)
I am looking for an easy way to use it NSAttributedString
with a very simple message field 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 was downloaded from the server, ensures that the text size fits the screen properly, and then tries to send NSAttributedString
to UIAlertView
. But I UIAlertView
don't like it. What would be the easiest way to solve this problem? (MOTD with HTML formatting is not an option)
source to share
Add your posted line to the shortcut and add it as an evaluation file for the warning
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 the days UIAlertView
are out of date. You can use UIAlertController
.
source to share
I only got the idea from @ AshishKakkad's answer (+1). However, its user interface is not displayed as expected. Therefore, I am showing you a way to format your post with attributedString
.
This is how I do 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;
}
source to share