2

我需要以斜体显示消息的某些部分而另一部分正常显示警报,如下所示:

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

部分写着:“这部分是斜体的。” 斜体,另一个是普通文本。

有可能吗?如果是,如何?

提前致谢!

4

2 回答 2

1

我最终创建了一个这样的本机警报控制器:

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);
        }
于 2017-12-05T13:49:01.063 回答
0

您可以尝试使用自定义渲染器来实现(使用自定义视图和渲染器来处理弹出窗口)但iOS 不建议这样做

警报传达与您的应用程序或设备状态相关的重要信息,并且经常请求反馈。警报由标题、可选消息、一个或多个按钮以及用于收集输入的可选文本字段组成。除了这些可配置元素之外,警报的视觉外观是静态的,无法自定义。

可能对您有帮助。

于 2017-11-30T12:47:29.663 回答