Xamarin.IOS UIAlertController 自定義字體

            var alertController =UIAlertController.Create ("", messageText, UIAlertControllerStyle.ActionSheet);

            var okButton = UIAlertAction.Create (okText, UIAlertActionStyle.Default,null);
            UIImage image = UIImage.FromBundle ("circle");
            okButton.SetValueForKey (image, new NSString ("image"));

            alertController.AddAction (okButton);


            var messageAttrite = new NSMutableAttributedString (messageText, UIFont.FromName ("GESSThree-Light", 17));
            alertController.SetValueForKey (messageAttrite,new NSString("attributedMessage"));

            var titleAttrite = new NSMutableAttributedString ("", UIFont.FromName ("GESSThree-Light", 17));

            alertController.SetValueForKey (titleAttrite,new NSString("attributedTitle"));


            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController (alertController, true, null);



方案二:自定義類,繼承UIAlertController

 public class CustomAlertView:UIAlertController
        {
            public override void ViewWillAppear (bool animated)
            {
                R (this.View);

                base.ViewWillAppear (animated);
            }

            private void R(UIView view)
            {
                foreach (var childView in view.Subviews) 
                {
                    if (childView is UIButton) {
                        ((UIButton)childView).Font = UIFont.FromName ("GESSThree-Light", 15);
                    }
                    if (childView is UILabel) {
                        ((UILabel)childView).Font = UIFont.FromName ("GESSThree-Light", 17);
                    }
                    if (childView.Subviews != null)
                    {
                        R (childView);
                    }
                }
            }
            public override UIAlertControllerStyle PreferredStyle {
                get {
                    return UIAlertControllerStyle.Alert;
                }
            }
        }


使用:

            var alertController =new CustomAlertView();
            alertController.Message = messageText;
            var okButton = UIAlertAction.Create (okText, UIAlertActionStyle.Default,null);
            alertController.AddAction (okButton);
            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController (alertController, true, null);

發佈了53 篇原創文章 · 獲贊 9 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章