Bold highlight a concatenated item

Asked

Viewed 279 times

1

I would like to highlight in bold a part of the text that is in the Displayalert notification.

inserir a descrição da imagem aqui

DisplayAlert("Alerta", "Deseja realmente incluir o lance de valor de R$ " + EntryLance.Text + "?", "Sim", "Não");

Obs: If you can give me an example in C# it will be of great value.

1 answer

1


I don’t think you’ll be able to put details of a RichText in the DisplayAlert shaman.

In the Soen there is one question, very similar, but it referred to italics, and the answer was to create a custom alert control that supports RichText (italics + bold).

Transcribing the solution given in this question:

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);
}

Qustan original no Soen: Xamarin.Forms - Displayalert with Italic font attribute

  • Oh yes, thank you. Too bad it’s complex, we were looking for something native, but by the way there really isn’t.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.