Xamarin Forms-带斜体字体属性的DisplayAlert
我需要显示一个警报,消息的一部分用斜体字显示,另一部分用普通字显示,如下所示:Xamarin Forms-带斜体字体属性的DisplayAlert,xamarin,xamarin.forms,popup,italic,Xamarin,Xamarin.forms,Popup,Italic,我需要显示一个警报,消息的一部分用斜体字显示,另一部分用普通字显示,如下所示: var title = "Title"; var body = "This part in Italic. This part normal."; Application.Current.MainPage.DisplayAlert(title, body, "OK"); void PromptRichTextPopup(string title, string richMessage, string normalM
var title = "Title";
var body = "This part in Italic. This part normal.";
Application.Current.MainPage.DisplayAlert(title, body, "OK");
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);
}
这部分写着:“这部分用斜体”,斜体,另一部分用普通文本
有可能吗?如果是,如何进行
提前谢谢 您可以尝试使用自定义渲染器来实现(使用自定义视图和渲染器来处理弹出窗口),但 警报传达与状态相关的重要信息 您的应用程序或设备,并经常请求反馈。警报包括 标题、可选消息、一个或多个按钮和可选 用于收集输入的文本字段。除了这些 元素时,警报的视觉外观是静态的,不能更改 定制的
可能会对您有所帮助。我最终创建了一个本机警报控制器,如下所示:
var title = "Title";
var body = "This part in Italic. This part normal.";
Application.Current.MainPage.DisplayAlert(title, body, "OK");
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);
}
作为“DisplayAlert”的替代方法,您可能希望使用自定义弹出控件。看看这个: