public static void SetImageFor(string imagePath, UIButton button, UIControlState state)
 {
     using (UIImage img = UIImage.FromFile(imagePath)) {
         Assert.NotNull(img);
         button.SetImage(img, state);
     }
 }
示例#2
0
 public virtual void SetTitleTextAttributes(TextAttributes attributes, UIControlState state)
 {
     if (attributes == null)
         throw new ArgumentNullException ("attributes");
     using (var dict = attributes.Dictionary){
         _SetTitleTextAttributes (dict, state);
     }
 }
 public MvxUIButtonTitleTargetBinding(UIButton button, UIControlState state = UIControlState.Normal)
     : base(button)
 {
     this._state = state;
     if (button == null)
     {
         MvxBindingTrace.Trace(MvxTraceLevel.Error, "Error - UIButton is null in MvxUIButtonTitleTargetBinding");
     }
 }
示例#4
0
            public void SetScopeBarButtonTitle(TextAttributes attributes, UIControlState state)
            {
                if (attributes == null)
                    throw new ArgumentNullException ("attributes");

                using (var dict = attributes.Dictionary) {
                    _SetScopeBarButtonTitle (dict, state);
                }
            }
        public static void SetTitle(this UIButton button, object source, PropertyInfo property,
            UIControlState state = UIControlState.Normal)
        {
            var text = property.GetValue(source).ToString();

            if (button.Title(state) != text)
            {
                button.InvokeOnMainThread(() => button.SetTitle(text, state));
            }
        }
示例#6
0
        public static Task<ImageDownloadResult> SetBackgroundImageAsync(this UIButton button, NSUrl url, UIControlState state, UIImage placeholder = null, SDWebImageOptions options = SDWebImageOptions.None)
        {
            var tcs = new TaskCompletionSource<ImageDownloadResult> ();

            SDWebImageManager.SharedManager.SetBackgroundImage (button, url, state, placeholder, options, (image, error, cacheType) => {
                if (image == null) {
                    tcs.SetException (new NSErrorException (error));
                    return;
                }
                tcs.SetResult (new ImageDownloadResult (image, cacheType));
            });

            return tcs.Task;
        }
        public static Action BindTitle(this UIButton button, 
                    INotifyPropertyChanged source, 
                    string propertyName,
                    UIControlState state = UIControlState.Normal)
        {
            var property = source.GetProperty(propertyName);

            button.SetTitle (source, property, state);

            var handler = new PropertyChangedEventHandler(
                (s, e) =>
                {
                    if (e.PropertyName == propertyName)
                    {
                        button.SetTitle (source, property, state);
                    }
                }
            );

            source.PropertyChanged += handler;

            return new Action(() => source.PropertyChanged -= handler);
        }
        /// <summary>
        /// Loads an image from a bundle given the supplied image name, resizes it to the
        /// height and width request and sets it into a <see cref="T:UIKit.UIButton"/>.
        /// </summary>
        /// <param name="source">The <see cref="T:Xamarin.Forms.ImageSource"/> to load the image from.</param>
        /// <param name="widthRequest">The requested image width.</param>
        /// <param name="heightRequest">The requested image height.</param>
        /// <param name="targetButton">A <see cref="T:UIKit.UIButton"/> to set the image into.</param>
        /// <param name="state">The state.</param>
        /// <param name="tintColor">Color of the tint.</param>
        private static async Task SetImageAsync(ImageSource source,
                                                int widthRequest,
                                                int heightRequest,
                                                UIButton targetButton,
                                                UIControlState state = UIControlState.Normal,
                                                UIColor tintColor = null)
        {
            var handler = GetHandler(source);
            using(var uiImage1 = await handler.LoadImageAsync(source, new CancellationToken(), 1f))
            {
                var uiImage2 = uiImage1;
                if(heightRequest > 0 && widthRequest > 0 && (uiImage1.Size.Height != heightRequest || uiImage1.Size.Width != widthRequest))
                {
                    // squish it so it fits within the size you requested.
                    uiImage2 = uiImage2.Scale(new CGSize(widthRequest, heightRequest));
                }

                var width = uiImage2.CGImage.Width;
                var uiImage3 = ClipToCircle(uiImage2, width, (float)width / 2);

                if(tintColor != null)
                {
                    targetButton.TintColor = tintColor;
                    targetButton.SetImage(uiImage3.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), state);
                }
                else
                {
                    targetButton.SetImage(uiImage3.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), state);
                }
            }
        }
示例#9
0
 // Don't override this method unless you also need to update some UI when the title text changes.
 public override void SetTitle(string title, UIControlState forState)
 {
     base.SetTitle(title, forState);
     Superview.SetNeedsLayout();
     Layer.CornerRadius = Layer.Frame.Height / 2;
 }
示例#10
0
        private UIButton ButtonInitializer(string title, UIControlState titleState, UIColor backgroundColor,
                                           UIColor titleColor, UIControlState colorTitleState, UIImage image, UIControlState imageState)
        {
            UIButton button = new UIButton();

            button.SetTitle(title, titleState);
            if (image != null)
            {
                button.SetImage(image, imageState);
            }
            button.BackgroundColor = backgroundColor;
            button.SetTitleColor(titleColor, colorTitleState);
            button.ClipsToBounds       = false;
            button.Layer.CornerRadius  = 10;
            button.Layer.ShadowColor   = UIColor.Black.CGColor;
            button.Layer.ShadowOpacity = 0.1f;
            button.Layer.ShadowRadius  = 1;
            button.Layer.ShadowOffset  = new CGSize(1, 1);
            return(button);
        }
示例#11
0
 public override void SetTitle(String title, UIControlState forState)
 {
     base.SetAttributedTitle(this.Compute(title), forState);
 }
示例#12
0
 public static UIButton SetTitleWithResize(this UIButton button, UIView view, string title, UIControlState state)
 {
     var frame = button.Frame;
     var newSize = view.StringSize (title, button.Font);
     var newFrame = new RectangleF (frame.Left, frame.Top, newSize.Width + 6, frame.Height);
     button.Frame = newFrame;
     button.SetTitle (title, state);
     return button;
 }
示例#13
0
        private async Task SetImageAsync(ImageSource source, int widthRequest, int heightRequest, UIButton targetButton, UIControlState state = UIControlState.Normal)
        {
            IImageSourceHandler handler = null;

            if (source is UriImageSource)
            {
                handler = new ImageLoaderSourceHandler();
            }
            else if (source is FileImageSource)
            {
                handler = new FileImageSourceHandler();
            }
            else if (source is StreamImageSource)
            {
                handler = new StreamImagesourceHandler();
            }

            if (handler != null)
            {
                using (UIImage image = await handler.LoadImageAsync(source, default(System.Threading.CancellationToken), (float)UIScreen.MainScreen.Scale))
                {
                    if (image != null)
                    {
                        UIImage scaled = image;
                        if ((heightRequest > 0) && (widthRequest > 0) && ((image.Size.Height != heightRequest) || (image.Size.Width != widthRequest)))
                        {
                            scaled = scaled.Scale(new CGSize(widthRequest, heightRequest), UIScreen.MainScreen.Scale);
                        }

                        if (ImageButton.SupportCheckedState && !ImageButton.Checked)
                        {
                            UIGraphics.BeginImageContextWithOptions(scaled.Size, false, scaled.CurrentScale);
                            var context = UIGraphics.GetCurrentContext();
                            var area    = new CGRect(0, 0, scaled.Size.Width, scaled.Size.Height);
                            context.ScaleCTM(1.0f, -1.0f);
                            context.TranslateCTM(0, -scaled.Size.Height);
                            context.SetBlendMode(CGBlendMode.Multiply);
                            context.SetAlpha((nfloat)ImageButton.UncheckedAlpha);
                            context.DrawImage(area, scaled.CGImage);

                            var newImage = UIGraphics.GetImageFromCurrentImageContext();
                            targetButton.SetImage(newImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), state);
                        }
                        else
                        {
                            targetButton.SetImage(scaled.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), state);
                        }
                    }
                }
            }
        }
示例#14
0
 public override String Title(UIControlState state)
 {
     return base.GetAttributedTitle(state).Value;
 }
示例#15
0
 public virtual TextAttributes GetTitleTextAttributes(UIControlState state)
 {
     using (var d = _GetTitleTextAttributes (state)){
         return new TextAttributes (d);
     }
 }
示例#16
0
        public static Task<ImageDownloadResult> SetBackgroundImageAsync(this UIButton button, string url, UIControlState state, UIImage placeholder = null, SDWebImageOptions options = SDWebImageOptions.None)
        {
            if (!Uri.IsWellFormedUriString (url, UriKind.Absolute)) throw new Exception (String.Format ("Malformed url: {0}", url));

            return SetImageAsync (button, NSUrl.FromString (url), state, placeholder, options);
        }
 /// <summary>
 /// Loads an image from a bundle given the supplied image name, resizes it to the
 /// height and width request and sets it into a <see cref="UIButton"/>.
 /// </summary>
 /// <param name="source">The <see cref="ImageSource"/> to load the image from.</param>
 /// <param name="widthRequest">The requested image width.</param>
 /// <param name="heightRequest">The requested image height.</param>
 /// <param name="targetButton">A <see cref="UIButton"/> to set the image into.</param>
 /// <returns>A <see cref="Task"/> for the awaited operation.</returns>
 private async static Task SetImageAsync(ImageSource source, int widthRequest, int heightRequest, UIButton targetButton, UIControlState state = UIControlState.Normal)
 {
     var handler = GetHandler(source);
     using (UIImage image = await handler.LoadImageAsync(source))
     {
         UIImage scaled = image;
         if (heightRequest > 0 && widthRequest > 0 && (image.Size.Height != heightRequest || image.Size.Width != widthRequest))
         {
             scaled = scaled.Scale(new CGSize(widthRequest, heightRequest));
         }
         targetButton.SetImage(scaled.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), state);   
     }
 }
示例#18
0
 public static void SetImage(this UIButton button, string url, UIControlState state, UIImage placeholder = null, SDWebImageOptions options = SDWebImageOptions.None, SDWebImageCompletedBlock completed = null)
 {
     if (!Uri.IsWellFormedUriString (url, UriKind.Absolute)) return;
     SDWebImageManager.SharedManager.SetImage (button, NSUrl.FromString (url), state, placeholder, options, completed);
 }
示例#19
0
 public static void SetImage(this UIButton button, NSUrl url, UIControlState state, UIImage placeholder = null, SDWebImageOptions options = SDWebImageOptions.None, SDWebImageCompletedBlock completed = null)
 {
     SDWebImageManager.SharedManager.SetImage (button, url, state, placeholder, options, completed);
 }
示例#20
0
 public TextAttributes GetScopeBarButtonTitleTextAttributes(UIControlState state)
 {
     using (var d = _GetScopeBarButtonTitleTextAttributes (state)) {
         return new TextAttributes (d);
     }
 }
示例#21
0
 public override void SetTitle(string title, UIControlState forState)
 {
     base.SetTitle(title, forState);
     UpdateLayout();
 }
示例#22
0
 public void SetTitleTextAttributes(TextAttributes attributes, UIControlState state)
 {
     using (var dict = attributes == null ? null : attributes.Dictionary)
         _SetTitleTextAttributes (dict, state);
 }
示例#23
0
        public virtual void MouseMove(Vector2 mouse)
        {
            if (IsOver(mouse.X, mouse.Y))
              {
            switch (State)
            {
              case UIControlState.Normal:
            State = UIControlState.Highlighted;
            break;
            }
              }
              else
              {
            switch (State)
            {
              case UIControlState.Highlighted:
            State = UIControlState.Normal;
            break;
            }
              }

              Game1.BlockNonUIEvents = IsOver(mouse.X, mouse.Y);
        }
示例#24
0
		public void SetBackgroundColor(UIColor color, UIControlState state){
			switch (state) {
				case UIControlState.Highlighted:
				case UIControlState.Selected:
					selectedColor = color;
					break;
				case UIControlState.Disabled:
					disabledColor = color;
					break;
				default:
					normalColor = color;
					break;
			}

			Initialize ();
		}
示例#25
0
 public TextAttributes GetTitleTextAttributes(UIControlState state)
 {
     using (var d = _GetTitleTextAttributes(state)) {
         return(new TextAttributes(d));
     }
 }