MBAlertView is a fun and simple block-based alert and HUD library for iOS, as seen in Notestand.
- Nested alerts and HUDs
- Block based
- Images
- Nice animations
- Doesn't use any PNG files. Everything is drawn with code.
// create an alert view with 'title' and 'message', using default colors, layout and no buttons
MBAlertView *alert = [MBAlertView alertWithTitle:title message:message];
// set the title font
alert.titleFont = [UIFont fontWithName:@"MyCustomFont-Regular" size:22];
// set custom background image for the alert view.
alert.backgroundImage = [UIImage imageNamed:@"Popup_Box_Background"];
// set custom body view for the alert body part. This allows e.g. to have a separate body-part background image or more complex view heirarchy for the body part only. Default is nil, in which case the alert will use the bodyText in the body area.
alert.customBodyView = [NSBundle mainBundle] loadNibNamed:@"AlertBodyView" owner:nil options:nil];
// By default the alert will mask the underlying view with semi-transparent black mask.
alert.addsMask = NO; // default is YES
// set the size for the alert view, in this case match the background image size
alert.size = alert.backgroundImage.size;
/* By default, the message of the alert is centered in the alert's bounds.
However, our Popup_Box_Background contains drop shadow,
which offsets the actual center from the visible center of the popup rect (without the shadow).
offset the content upward a bit by setting contentEdgeInsets
*/
alert.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 14, 0);
// add custom cancel button
UIImage *image = [UIImage imageNamed:@"Cancel_Button_Background"];
UIButton *button = [[UIButton alloc] initWithFrame:(CGRect){CGPointZero, image.size}];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:@"MyCustomFont-Regular" size:16]];
[button setTitle:cancelTitle forState:UIControlStateNormal];
[alert addCustomButton:button block:^{
// any button automatically dismisses the alert view
// this block is useful for additional handler code after the alert is dismissed.
}];
[alert addToDisplayQueue];
There are two factory methods to get you started:
// Create an alert with a standard canel button
+(MBAlertView*)alertWithTitle:(NSString*)title message:(NSString*)body cancelTitle:(NSString*)cancelTitle cancelBlock:(id)cancelBlock;
// Create an alert with no buttons (you can add buttons using addButtonWithText: or addCustomButton:)
+(MBAlertView*)alertWithTitle:(NSString*)title message:(NSString*)body;
[alert addToDisplayQueue];
[MBHUDView hudWithBody:@"Wait." type:MBAlertViewHUDTypeActivityIndicator hidesAfter:4.0 show:YES];
You can see more in the easy to follow demo.
MBAlertView is available under the MIT license.