格式化 UILocalNotification.alertBody 的文本

Format text of UILocalNotification.alertBody

我正在寻找一种方法来格式化 alertBodyUILocalNotification 的文本。我注意到,例如邮件应用程序从主题设置第一行,第二行和第三行是电子邮件正文的预览。如果主题超过 1 行,则会被截断。这适用于动态字体,因此无论辅助功能中设置的文本大小如何。 我试图达到同样的结果,但没有成功。

我试过的

  1. 将字符串截断为特定长度
  2. 在使用 preferedFontForTextStyle
  3. 时使用 sizeWithAttributes 获取字符串大小

所有这些解决方案都部分有效,但对于某些文本 size/device 会失败

我正在寻找的是一种获取锁定屏幕的通知字体名称、大小、字距调整或任何其他必要参数的方法,以便能够将 UILocalNotification.alertBody 正确设置为任何 [= 上的仅一行文本35=] 具有任何文本大小辅助功能设置的设备。

我知道这些属性对于不同的 iOS 版本可能不同,但是 iOS8 对我来说已经足够了。

谢谢

apples邮件应用第一行是alertTitle属性,最近推出>=iOS8.22015-03-09See here.

Use this property to provide a short description of the reason for the alert. You may specify a string with the text you want to display or you may specify a string to use as a lookup key in your app’s Localizable.strings file. The default value of this property is nil.

Title strings should be short, usually only a couple of words describing the reason for the notification. Apple Watch displays the title string as part of the short look notification interface, which has limited space.

根据你的第二个注释,所有通知的 alertBody 字符串都被截断了,具体取决于它的长度。通知仅显示这么多字符以补充 UILocalNotification 特定高度。但是,无论大小,如果你下拉通知,它会显示剩余的字符,只要有任何字符,就像苹果的邮件应用程序一样。您 可以 使用任何 method that works for you 来缩短您的 alertBody,但是就根据 UIAppearance 协议对其进行格式化而言,Apple 尚未批准任何方法

让 OS 进行布局

你最接近的是一个猜测。 您可以使用

"\n"

强制执行回车 return,但您肯定不知道何时使用它。

没有 iOS 支持让您提前告知您的通知将到达的屏幕尺寸或方向。

你反对横幅:

横幅不显示localNotification.alertTitle

然后争分夺秒,可以这么说:

您提前创建通知。它们是本地的这一事实没有理由知道未来会发生什么:就你所知,你可能会在 1 个月后开始安排,并且用户可能已经更新了 OS,导致新字体或布局限制申请。

换句话说,这个问题的答案是: 您需要为 Apple Computer 工作并在显示通知之前获得本地回调,到目前为止,这不是一个选项。


结论

使用short alertTitle, short alertBody, 可能到处都是\n 强制换行。

// Use '\n' to force newline
localNotification.alertBody = "Alert fired.\nWas set for ..."