UIActionSheet/UIAlertController 多行文本

6

这是我编写的用于显示 UIActionSheet 的代码。

actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"updateresponseforrecurring", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) destructiveButtonTitle:nil otherButtonTitles:
                   NSLocalizedString(@"updateresponseonlyforthis", nil),
                   NSLocalizedString(@"updateresponseforallactivities", nil),
                   nil];
    actionSheet.tag = 2;
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];  

这是我使用此代码得到的结果:

enter image description here

显然,第二个选项更长,因此大小会变小以适应宽度。
但我希望所有选项都有相同的字体大小,这只能使用多行文本实现。也尝试过使用UIAlertController,但无法设置多行文本。 如何实现?

4个回答

27

这似乎适用于iOS 10和Swift 3.1:

UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).numberOfLines = 2

1
您还可以将numberOfLines设置为0。这将允许尽可能多的行。 - Berni

2
尝试这个。
let alert = UIAlertController(title: title,
                              message: "you message go here",
                              preferredStyle: 
UIAlertControllerStyle.alert)

let cancelAction = UIAlertAction(title: "Cancel",
                                 style: .cancel, handler: nil)

alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)

UILabel.appearance(whenContainedInInstancesOf: 
[UIAlertController.self]).numberOfLines = 0

UILabel.appearance(whenContainedInInstancesOf: 
[UIAlertController.self]).lineBreakMode = .byWordWrapping

虽然这段代码可能回答了问题,但是提供关于为什么和/或如何回答问题的额外上下文可以提高其长期价值。 - rollstuhlfahrer

1
这是使用标准的UIAlertControllerUIAlertView无法实现的。 我建议您将其缩短。为什么不创建一个警报,然后输入类似以下内容的文字:
“您想仅更新此实例还是更新此系列中的所有活动。”
答案如下:
- 仅此实例 - 所有活动

如果我的回答帮助您解决了问题,请将其选择为接受的答案。谢谢! - Fabio Berger

1
在iOS 10中:
如果您想将其应用于所有UIAlertController,可以使用以下代码行:
[[UILabel appearanceWhenContainedIn:[UIAlertController class], nil] setNumberOfLines:2];
[[UILabel appearanceWhenContainedIn:[UIAlertController class], nil] setFont:[UIFont systemFontOfSize:9.0]];

将此代码放入AppDelegate的didFinishLaunchingWithOptions方法中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接