2

I need to display an alert with some part of the message in italics and the other part normal, something like this:

var title = "Title";
var body = "This part in Italic. This part normal.";
Application.Current.MainPage.DisplayAlert(title, body, "OK");

With the part that says: "This part in Italic." in italics and the other one in normal text.

Is it possible to do? If yes, how?

Thanks in advance!

Cesar Tomatis
  • 598
  • 1
  • 4
  • 15
  • As an alternative for 'DisplayAlert' you may want to use a custom pop up control instead. Take a look @ this: https://github.com/rotorgames/Rg.Plugins.Popup – EvZ Nov 30 '17 at 13:13

2 Answers2

1

I ended up creating a native alert controller like this:

void PromptRichTextPopup(string title, string richMessage, string normalMessage, Action onOkCallback, Action onCancel = null) {
            var vc = UIKit.UIApplication.SharedApplication.KeyWindow.RootViewController;
            // take top presented view controller
            while (vc.PresentedViewController != null) {
                vc = vc.PresentedViewController;
            }

            var alertvc = UIAlertController.Create(title, string.Empty, UIAlertControllerStyle.Alert);
            var leftAligned = new NSMutableParagraphStyle();
            leftAligned.Alignment = UITextAlignment.Left;

            var colorTitle = new NSAttributedString(str: title, font: UIFont.BoldSystemFontOfSize(18), foregroundColor: Xamarin.Forms.Color.FromHex("#61acad").ToUIColor());

            alertvc.SetValueForKey(colorTitle, new NSString("attributedTitle"));

            var margin = 5f;
            var height = 30f;
            var width = 256f;

            var container = new UIView(new CGRect(margin, margin, width, height * 4));

            var message = new NSMutableAttributedString(str: richMessage, font: UIFont.ItalicSystemFontOfSize(14), foregroundColor: UIColor.Black);
            message.Append(new NSMutableAttributedString(str: " " + normalMessage, font: UIFont.SystemFontOfSize(14), foregroundColor: UIColor.Black));
            var lblText = new UILabel(new CGRect(0, -(height / 2), width, height * 2)) { AttributedText = message };
            lblText.LineBreakMode = UILineBreakMode.WordWrap;
            lblText.Lines = 0;
            container.AddSubview(lblText);

            var cancel = new UIButton(new CGRect(0, height, width / 2, height * 2));
            cancel.SetTitle("NO", UIControlState.Normal);
            cancel.AddTarget((sender, e) => alertvc.DismissViewController(true, null), UIControlEvent.TouchUpInside);
            cancel.SetTitleColor(UIColor.Blue, UIControlState.Normal);
            if (onCancel != null) {
                cancel.AddTarget((sender, e) => {
                    onCancel();
                },
                UIControlEvent.TouchUpInside);
            }
            container.AddSubview(cancel);

            var ok = new UIButton(new CGRect(width / 2, height, width / 2, height * 2));
            ok.SetTitle("YES", UIControlState.Normal);
            Action okAction = async () => {
                ok.Enabled = false;
                await uiHelper.RunBlocking(() => {
                    onOkCallback();
                });
                alertvc.DismissViewController(true, null);
            };
            ok.SetTitleColor(UIColor.Blue, UIControlState.Normal);
            container.AddSubview(ok);
            ok.AddTarget((sender, e) => {
                okAction();
            }, UIControlEvent.TouchUpInside);

            var controller = new UIViewController();
            controller.View.AddSubview(container);
            alertvc.SetValueForKey(controller, new NSString("contentViewController"));
            vc.PresentViewController(alertvc, true, null);
        }
Cesar Tomatis
  • 598
  • 1
  • 4
  • 15
0

You may try to use Custom Renderer to achieve (use a custom View and a renderer for handling a popup) it but it is not recommended by iOS

Alerts convey important information related to the state of your app or the device, and often request feedback. An alert consists of a title, an optional message, one or more buttons, and optional text fields for gathering input. Aside from these configurable elements, the visual appearance of an alert is static and can’t be customized.

This may be helpful for you.

Json
  • 1,655
  • 1
  • 13
  • 21