警告提示視圖
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; } } }}