Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format text of UILocalNotification.alertBody

I am looking for a way how to format text of alertBody of UILocalNotification. I noticed that for example mail app sets first line from Subject and second and third lines are preview of email body. If subject is longer than 1 line, it is truncated. This works correctly with dynamic font, so no matter what text size is set in accessibility. I tried to do achieve same result, but with no success.

What I tried

  1. Truncate string to specific length
  2. Getting string size with sizeWithAttributes while using preferedFontForTextStyle

All these solutions partially work, but for some text size/device will fail

What I am looking for is an approach to get lock screen's notification font name, size, kerning or any other parameters neccessary to be able to properly set UILocalNotification.alertBody as only 1 line of text on any iOS device with any text size accessibility setting.

I am aware that these properties may be different for different iOS version, but iOS8 is enough for me.

Thank you

like image 596
Petr Avatar asked Aug 26 '15 10:08

Petr


1 Answers

Let the OS do the layout

The closest you are going to get to is a guess. You can use

"\n"

to enforce a carriage return, but you won't know for sure when to use it.

There is no iOS support to let you tell, ahead of time, what is the screen size or orientation your notification will arrive.

You are up against the banner:

Banners do not display localNotification.alertTitle

2 notification banners, portrait and landscape

Then up against the clock, so to speak:

You create notifications ahead of time. The fact that they are local is no reason to know what the future will hold: for all you know, you may be scheduling 1 month from now, and the user may have updated the OS, causing new fonts or layout constraints to apply.

In other words, the answer to this question is: You need to work for Apple Computer and get a local callback prior the notification is displayed, which is, as of date, not an option..

Notification Center


Conclusion

Use short alertTitle, short alertBody, possibly sprinkled here and there with \n to force new lines.

// Use '\n' to force newline
localNotification.alertBody = "Alert fired.\nWas set for ..."
like image 129
SwiftArchitect Avatar answered Oct 06 '22 04:10

SwiftArchitect