Пример #1
0
        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.BackgroundColor=UIColor.Black.ColorWithAlpha(kOpacity);

            if(image != null)
            {
                imageView = new UIImageView(image);
                imageView.ContentMode=UIViewContentMode.ScaleAspectFit;
                imageView.Frame=CGRectMake(kHorizontalPadding, kVerticalPadding, kImageWidth, kImageHeight);
            }

            float imageWidth, imageHeight, imageLeft;

            // the imageView frame values will be used to size & position the other views
            if(imageView != null)
            {
                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;

                NSString titleS = new NSString(title);
                // size the title label according to the length of the text
                SizeF maxSizeTitle = CGSizeMake((self.Bounds.Size.Width * kMaxWidth) - imageWidth, self.Bounds.Size.Height * kMaxHeight);
                SizeF 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.LineBreakMode=UILineBreakMode.WordWrap;
                messageLabel.TextColor=UIColor.White;
                messageLabel.BackgroundColor=UIColor.Clear;
                messageLabel.Alpha=1.0f;
                messageLabel.Text=message;

                // size the message label according to the length of the text
                NSString messageS = new NSString(message);
                SizeF maxSizeMessage = CGSizeMake((self.Bounds.Size.Width * kMaxWidth) - imageWidth, self.Bounds.Size.Height * kMaxHeight);
                SizeF expectedSizeMessage = messageS.StringSize(messageLabel.Font,maxSizeMessage,messageLabel.LineBreakMode);
                messageLabel.Frame=CGRectMake(0.0f, 0.0f, expectedSizeMessage.Width, expectedSizeMessage.Height);
            }

            // titleLabel frame values
            float 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
            float 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;
            }

            float longerWidth = Math.Max(titleWidth, messageWidth);
            float longerLeft = Math.Max(titleLeft, messageLeft);

            // wrapper width uses the longerWidth or the image width, whatever is larger. same logic applies to the wrapper height
            float wrapperWidth = Math.Max((imageWidth + (kHorizontalPadding * 2)), (longerLeft + longerWidth + kHorizontalPadding));
            float wrapperHeight = Math.Max((messageTop + messageHeight + kVerticalPadding), (imageHeight + (kVerticalPadding * 2)));

            wrapperView.Frame=CGRectMake(0.0f, 0.0f, wrapperWidth, wrapperHeight);

            if(titleLabel != null)
            {
                titleLabel.Frame=CGRectMake(titleLeft, titleTop, titleWidth, titleHeight);
                wrapperView.AddSubview(titleLabel);
            }

            if(messageLabel != null)
            {
                messageLabel.Frame=CGRectMake(messageLeft, messageTop, messageWidth, messageHeight);
                wrapperView.AddSubview(messageLabel);
            }

            if(imageView != null)
            {
                wrapperView.AddSubview(imageView);
            }

            return wrapperView;
        }