Advertisement
Guest User

UIConversationBubbleView

a guest
Mar 20th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.61 KB | None | 0 0
  1.     using CoreGraphics;
  2.     using Foundation;
  3.     using Common.Utilities;
  4.     using System;
  5.     using System.Drawing;
  6.     using UIKit;
  7.  
  8.     namespace Messenger.iOS.Controls.ConversationBubble
  9.     {
  10.         [Register("UIConversationBubbleView")]
  11.         class UIConversationBubbleView : UIView
  12.         {
  13.             #region Layout Variables
  14.             // Defining colours and margins
  15.             ...
  16.             #endregion
  17.  
  18.             #region iOS Properties
  19.             private static NSString classId = new NSString("UIConversationBubbleView");
  20.             public static NSString ClassId { get { return classId; } }
  21.             #endregion
  22.  
  23.             #region Data Properties
  24.             private string mMessageBody;
  25.             public string MessageBody { get { return mMessageBody;  } set { mMessageBody = value; SetNeedsDisplay(); } }
  26.  
  27.             private DateTime mTime;
  28.             public DateTime Time { get { return mTime; } set { mTime = value; SetNeedsDisplay(); } }
  29.  
  30.             public string mDirection;
  31.             public string Direction { get { return mDirection; } set { mDirection = value; SetNeedsDisplay(); } }
  32.             #endregion
  33.  
  34.             #region Content Views
  35.             private UILabel mDirectionLabel;
  36.             private UILabel mMessageBodyLabel;
  37.             private UILabel mTimeLabel;
  38.             #endregion
  39.  
  40.             #region Initialization
  41.             public UIConversationBubbleView(string body, DateTime time, string direction)
  42.             {
  43.                 TranslatesAutoresizingMaskIntoConstraints = false;
  44.  
  45.                 mMessageBody = body;
  46.                 mTime = time;
  47.                 mDirection = direction;
  48.                
  49.                 InitializeSubviews();
  50.             }
  51.  
  52.             private void InitializeSubviews() {
  53.                 // init message direction label
  54.                 mDirectionLabel = new UILabel();
  55.                 AddSubview(mDirectionLabel);
  56.  
  57.                 mDirectionLabel.TranslatesAutoresizingMaskIntoConstraints = false;
  58.                 mDirectionLabel.TextAlignment = UITextAlignment.Left;
  59.                 mDirectionLabel.TextColor = UIColor.FromCGColor(Direction == "MO" ? MOColor : MTColor);
  60.                 mDirectionLabel.LayoutMargins = new UIEdgeInsets(0, 0, 0, 0);
  61.  
  62.                 // init message body label
  63.                 mMessageBodyLabel = new UILabel();
  64.                 AddSubview(mMessageBodyLabel);
  65.                
  66.                 mMessageBodyLabel.TranslatesAutoresizingMaskIntoConstraints = false;
  67.                 mMessageBodyLabel.LineBreakMode = UILineBreakMode.WordWrap;
  68.                 mMessageBodyLabel.TextColor = UIColor.Black;
  69.                 mMessageBodyLabel.Lines = 0;
  70.                 mMessageBodyLabel.LayoutMargins = new UIEdgeInsets(0, 0, 0, 0);
  71.  
  72.                 // init the timestamp label
  73.                 mTimeLabel = new UILabel();
  74.                 AddSubview(mTimeLabel);
  75.  
  76.                 mTimeLabel.TranslatesAutoresizingMaskIntoConstraints = false;
  77.                 mTimeLabel.TextColor = UIColor.LightGray;
  78.                 mTimeLabel.TextAlignment = UITextAlignment.Left;
  79.                 mTimeLabel.LayoutMargins = new UIEdgeInsets(0, 0, 0, 0);
  80.  
  81.                 // Set the Text values
  82.                 mDirectionLabel.Text = Direction;
  83.                 mMessageBodyLabel.Text = MessageBody;
  84.                 mTimeLabel.Text = GetDateString(Time);
  85.  
  86.                 AddSubviewConstraints();
  87.  
  88.                 SetNeedsDisplay();
  89.                 LayoutSubviews();
  90.             }
  91.  
  92.             private void AddSubviewConstraints() {
  93.                 var leftConstraint = (mDirection == "MO") ? BUBBLE_INDENT_MARGIN : BUBBLE_MARGIN;
  94.                 var rightConstraint = (mDirection == "MO") ? BUBBLE_MARGIN : BUBBLE_INDENT_MARGIN;
  95.  
  96.                 // Parent view must stretch to contain subviews.
  97.                 //AddConstraint(NSLayoutConstraint.Create(this, NSLayoutAttribute.Top, NSLayoutRelation.Equal, mDirectionLabel, NSLayoutAttribute.Top, 1, 0));
  98.                 AddConstraint(NSLayoutConstraint.Create(this, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, mTimeLabel, NSLayoutAttribute.Bottom, 1, BUBBLE_MARGIN));
  99.  
  100.                 AddConstraint(NSLayoutConstraint.Create(this, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, mDirectionLabel, NSLayoutAttribute.Width, 1, BUBBLE_WIDTH_OFFSET));
  101.                 //AddConstraint(NSLayoutConstraint.Create(this, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, mMessageBodyLabel, NSLayoutAttribute.Height, 1, BUBBLE_MARGIN * 2));
  102.  
  103.                 // Direction Label is indented if MO (This affects the other labels because they have constraints that depend on this label.) and set to the width minus the indent and margins.
  104.                 AddConstraint(NSLayoutConstraint.Create(mDirectionLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1, BUBBLE_MARGIN));
  105.                 AddConstraint(NSLayoutConstraint.Create(mDirectionLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, leftConstraint));
  106.                 AddConstraint(NSLayoutConstraint.Create(mDirectionLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, rightConstraint));
  107.                
  108.                 //AddConstraint(NSLayoutConstraint.Create(mDirectionLabel, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, 1, mDirectionLabel.Font.LineHeight));
  109.  
  110.                 // Body label is below the direction label, and has the same width and left position.
  111.                 AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, mDirectionLabel, NSLayoutAttribute.Bottom, 1, 0));
  112.                 AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, leftConstraint));
  113.                 AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, rightConstraint));
  114.  
  115.                 //AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1, -BUBBLE_WIDTH_OFFSET));
  116.                 //AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, 1, mMessageBodyLabel.Font.LineHeight));
  117.  
  118.                 // Time label is below the message body, and has the same width and left position.
  119.                 AddConstraint(NSLayoutConstraint.Create(mTimeLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, mMessageBodyLabel, NSLayoutAttribute.Bottom, 1, 0));
  120.                 AddConstraint(NSLayoutConstraint.Create(mTimeLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, leftConstraint));
  121.                 AddConstraint(NSLayoutConstraint.Create(mTimeLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, rightConstraint));
  122.  
  123.                 //AddConstraint(NSLayoutConstraint.Create(mTimeLabel, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, 1, mTimeLabel.Font.LineHeight));
  124.             }
  125.             #endregion
  126.  
  127.             # region UIView overrides
  128.             public override void LayoutSubviews()
  129.             {
  130.                 mMessageBodyLabel.PreferredMaxLayoutWidth = Bounds.Size.Width - BUBBLE_WIDTH_OFFSET;
  131.                 base.LayoutSubviews();
  132.             }
  133.             #endregion
  134.  
  135.             #region Helpers
  136.             private String GetDateString(DateTime date)
  137.             { ... }
  138.             #endregion
  139.         }
  140.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement