iPhone视图控件之--UIAlertView
警告提示视图
NSString *content = NSLocalizedString(@"this is test alert message", nil); UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert title", nil) message:content delegate:self cancelButtonTitle:NSLocalizedString(@"cancel", nil) otherButtonTitles:NSLocalizedString(@"ok", nil), nil]; alert.tag = 1; //用来作为标识,可区分不同的提示框 [alert show];
在使用的类里,实现代理协议UIAlertViewDelegate
按钮点击处理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { switch(buttonIndex){ case 0: //do cancel break; case 1: //do ok break; } }
在UIAlertView显示动画之前,改变提示message的对其方式,改为左对齐
- (void)willPresentAlertView:(UIAlertView *)alertView { //遍历UIAlertView的子视图,改变label的大小,尺寸,和对齐方式 for(UIView *subview in alertView.subviews) { if([[subview class] isSubclassOfClass:[UILabel class]]) { if(![((UILabel *)subview).text isEqualToString:iGexinLocalizedString(@"file detail", nil)]){ UILabel *label = (UILabel*)subview; CGRect rect = CGRectInset(label.frame, 20, 0); //label边界留20 NSString * labelText = label.text; CGFloat oldH = rect.size.height; rect.size = [labelText sizeWithFont:label.font constrainedToSize: CGSizeMake(rect.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; //计算文本的宽度和高度 if(oldH < rect.size.height){ rect.origin.y -= 10; } label.frame = rect; //调整label的位置和尺寸 label.textAlignment = UITextAlignmentLeft; //label文本左对齐 break; } } } }