2

这是我的代码

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Added to Cart" message:@"Some items are added for time being!" delegate:self cancelButtonTitle:@"View Cart" otherButtonTitles:@"Continue \n Shopping", nil];
alert.tag = 20;
[alert show];

我得到这样的输出:
在此处输入图像描述

我需要这样:
在此处输入图像描述

4

4 回答 4

2

我有手动解决方案给你。但我认为这不是一个好的解决方案:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Added to Cart" message:@"Some items are added for time being!" delegate:self cancelButtonTitle:@"View Cart" otherButtonTitles:@"", nil];

    UILabel *buttonTitle = [[UILabel alloc] initWithFrame:CGRectMake(148, 102, 125, 40)];
    buttonTitle.text = @"Continue Shopping";
    buttonTitle.font = [UIFont boldSystemFontOfSize:15];
    buttonTitle.textColor = [UIColor whiteColor];
    buttonTitle.textAlignment = UITextAlignmentCenter;
    buttonTitle.backgroundColor = [UIColor clearColor];
    buttonTitle.numberOfLines = 2;
    [alert addSubview:buttonTitle];
    alert.tag = 20;
    [buttonTitle release];
    [alert show];

无论如何你都可以使用它......

于 2012-05-24T11:33:09.057 回答
1

UIAlertView不支持多行按钮。一种选择是仅使用“继续”而不是“继续购物”作为按钮标题,否则您必须使用自定义警报视图组件,例如CODialog(您可能需要对其进行一些自定义以允许多行按钮,但应该很容易)。

于 2012-05-24T10:49:32.440 回答
0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Added to Cart" message:@"Some items are added for time being!" delegate:self cancelButtonTitle:@"View Cart" otherButtonTitles:@"Continue \n Shopping", nil];
alert.tag = 20;
[[[alert buttons] objectAtIndex:1] setLineBreakMode:UILineBreakModeWordWrap];
[alert show];

这段代码完全符合您的需要。但请注意,

  1. - [UIAlertView buttons]无证
  2. - [UIButton setLineBreakMode]已弃用。但是,您可以将其替换为

    [button.titleLabel setLineBreakMode]这基本上是相同的。

于 2012-05-24T10:51:42.760 回答
0

我认为您可以尝试将 numbersOfLine 属性设置为按钮上的标签。但我不知道苹果会同意这一点。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Added to Cart" message:@"Some items are added for time being!" delegate:self cancelButtonTitle:@"View Cart" otherButtonTitles:@"Continue \n Shopping", nil];
NSArray *subviewsArray = [alert subviews];
    for (UIView *subview in subviewsArray) {
        if ([subview isKindOfClass:[UIButton class]]) {
            NSArray *btnSubviews = [subview subviews];
            for (UIView *btnSubview in btnSubviews) {
                if ([btnSubview isKindOfClass:[UILabel class]]) {
                    UILabel *title = (UILabel *)btnSubview;
                    title.numberOfLines = 2;
                }
            }
        }
    }
alert.tag = 20;
[alert show];
[alert release];
于 2012-05-24T10:51:44.787 回答