public override void SetVirtualView(IView view) { base.SetVirtualView(view); #if ANDROID ContainerView = new WrapperView(Context); #else ContainerView = new WrapperView(); #endif }
public static UIView MakeViewForMessage(this UIView self,string message,string title,UIImage image) { /*********************************************************************************** * * * Dynamically build a toast view with any combination of message, title, & image. * * * ***********************************************************************************/ if((message == null) && (title == null) && (image == null)) return null; UILabel messageLabel = null; UILabel titleLabel = null; UIImageView imageView = null; // create the parent view WrapperView wrapperView = new WrapperView(); wrapperView.AutoresizingMask=(UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleBottomMargin); wrapperView.Layer.CornerRadius=kCornerRadius; if (kDisplayShadow) { wrapperView.Layer.ShadowColor=UIColor.Black.CGColor; wrapperView.Layer.ShadowOpacity=0.8f; wrapperView.Layer.ShadowRadius=6.0f; wrapperView.Layer.ShadowOffset=CGSizeMake(4.0f, 4.0f); } wrapperView.UserInteractionEnabled = false; wrapperView.BackgroundColor=UIColor.Black.ColorWithAlpha(kOpacity); nfloat imageWidth, imageHeight, imageLeft; if(image != null) { imageView = new UIImageView(image); imageView.ContentMode=UIViewContentMode.ScaleAspectFit; imageView.Frame=CGRectMake(kHorizontalPadding, kVerticalPadding, kImageWidth, kImageHeight); imageWidth = imageView.Bounds.Size.Width; imageHeight = imageView.Bounds.Size.Height; imageLeft = kHorizontalPadding; } else { imageWidth = imageHeight = imageLeft = 0.0f; } if (title != null) { titleLabel = new UILabel(); titleLabel.Lines = kMaxTitleLines; titleLabel.Font = UIFont.BoldSystemFontOfSize(kFontSize); titleLabel.TextAlignment=UITextAlignment.Left; titleLabel.LineBreakMode=UILineBreakMode.WordWrap; titleLabel.TextColor=UIColor.White; titleLabel.BackgroundColor=UIColor.Clear; titleLabel.Alpha=1.0f; titleLabel.Text=title; titleLabel.TextAlignment = TextAlignment; NSString titleS = new NSString(title); // size the title label according to the length of the text CGSize maxSizeTitle = CGSizeMake((self.Bounds.Size.Width * kMaxWidth) - imageWidth, self.Bounds.Size.Height * kMaxHeight); CGSize expectedSizeTitle = titleS.StringSize(titleLabel.Font,maxSizeTitle,titleLabel.LineBreakMode); titleLabel.Frame=CGRectMake(0.0f, 0.0f, expectedSizeTitle.Width, expectedSizeTitle.Height); } if (message != null) { messageLabel = new UILabel(); messageLabel.Lines=kMaxMessageLines; messageLabel.Font=UIFont.SystemFontOfSize(kFontSize); messageLabel.AutoresizingMask = UIViewAutoresizing.None; messageLabel.LineBreakMode=UILineBreakMode.WordWrap; messageLabel.TextColor=UIColor.White; messageLabel.BackgroundColor=UIColor.Clear; messageLabel.Alpha=1.0f; messageLabel.Text=message; messageLabel.TextAlignment = TextAlignment; // size the message label according to the length of the text NSString messageS = new NSString(message); CGSize maxSizeMessage = CGSizeMake((self.Bounds.Size.Width * kMaxWidth) - imageWidth, self.Bounds.Size.Height * kMaxHeight); CGSize expectedSizeMessage = messageS.StringSize(messageLabel.Font,maxSizeMessage,messageLabel.LineBreakMode); messageLabel.Frame=CGRectMake(0.0f, 0.0f,Round(expectedSizeMessage.Width),Round(expectedSizeMessage.Height)); } // titleLabel frame values nfloat titleWidth, titleHeight, titleTop, titleLeft; if(titleLabel != null) { titleWidth = titleLabel.Bounds.Size.Width; titleHeight = titleLabel.Bounds.Size.Height; titleTop = kVerticalPadding; titleLeft = imageLeft + imageWidth + kHorizontalPadding; } else { titleWidth = titleHeight = titleTop = titleLeft = 0.0f; } // messageLabel frame values nfloat messageWidth, messageHeight, messageLeft, messageTop; if(messageLabel != null) { messageWidth = messageLabel.Bounds.Size.Width; messageHeight = messageLabel.Bounds.Size.Height; messageLeft = imageLeft + imageWidth + kHorizontalPadding; messageTop = titleTop + titleHeight + kVerticalPadding; } else { messageWidth = messageHeight = messageLeft = messageTop = 0.0f; } var longerWidth = Math.Max(titleWidth, messageWidth); var longerLeft = Math.Max(titleLeft, messageLeft); // wrapper width uses the longerWidth or the image width, whatever is larger. same logic applies to the wrapper height var wrapperWidth = Math.Max((imageWidth + (kHorizontalPadding * 2)), (longerLeft + longerWidth + kHorizontalPadding)); var wrapperHeight = Math.Max((messageTop + messageHeight + kVerticalPadding), (imageHeight + (kVerticalPadding * 2))); wrapperView.Frame=CGRectMake((nfloat)0.0, (nfloat)0.0, (nfloat)wrapperWidth, (nfloat)wrapperHeight); if(titleLabel != null) { wrapperView.AddTitleLabel(titleLabel); } if(messageLabel != null) { wrapperView.AddMessageLabel(messageLabel); } if(imageView != null) { wrapperView.AddImageView (imageView); } return wrapperView; }