如何在UIAlertView中更改消息的对话?

时间:2015-11-12 06:25:26

标签: ios css uialertview

当我使用UIAlertView时,我发现消息的textAligment是Center。例如,当我写这段代码时:

function getShortMD5Hash(input) {
  var rawHash = Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, input);
  var txtHash = '';
    for (j = 0; j < 16; j += 8) { 
    hashVal = (rawHash[j] + rawHash[j+1] + rawHash[j+2] + rawHash[j+3]) ^ (rawHash[j+4] + rawHash[j+5] + rawHash[j+6] + rawHash[j+7])
    if (hashVal < 0)
      hashVal += 1024;
    if (hashVal.toString(36).length == 1)
      txtHash += "0";
    txtHash += hashVal.toString(36);
  }
    return txtHash.toUpperCase();
  }

效果如下:enter image description here
正如我们所见,文本&#34;这是第一行&#34;是在中心,但我想把它改成左边。我知道UILabel可以改变文本的对齐方式,但我不知道是否可以在UIAlertView中更改它。

2 个答案:

答案 0 :(得分:2)

像这样编辑你的代码。

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil
                                               message:nil
                                              delegate:nil
                                     cancelButtonTitle:nil
                                     otherButtonTitles:nil, nil];


UILabel *v = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
v.text = NSTextAlignmentLeft;
v.numberOfLines = 0;
v.text = @"this is first line\nsecondLine should be center";
[alert setValue:v forKey:@"accessoryView"];
[alert show];

答案 1 :(得分:1)

iOS7:对于左对齐,您可以这样做:

NSArray *subViewArray = alertView.subviews;
for(int x = 0; x < [subViewArray count]; x++){

    //If the current subview is a UILabel...
    if([[[subViewArray objectAtIndex:x] class] isSubclassOfClass:[UILabel class]]) {
        UILabel *label = [subViewArray objectAtIndex:x];
        label.textAlignment = NSTextAlignmentLeft;
    }
}

参考:How to make a multiple line, left-aligned UIAlertView?

或者您可以将UILabel添加到alertView:

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Centered Title" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12.0, 24.0, 250.0, 80.0)];
    label.numberOfLines = 0;
    label.textAlignment = NSTextAlignmentLeft;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.text = @"Here is an example of a left aligned label in a UIAlertView!";
    [alert addSubview:label];
    [alert show];

参考:How to align only the title in UIAlertView