Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using CoreGraphics;
- using Foundation;
- using Common.Utilities;
- using System;
- using System.Drawing;
- using UIKit;
- namespace Messenger.iOS.Controls.ConversationBubble
- {
- [Register("UIConversationBubbleView")]
- class UIConversationBubbleView : UIView
- {
- #region Layout Variables
- // Defining colours and margins
- ...
- #endregion
- #region iOS Properties
- private static NSString classId = new NSString("UIConversationBubbleView");
- public static NSString ClassId { get { return classId; } }
- #endregion
- #region Data Properties
- private string mMessageBody;
- public string MessageBody { get { return mMessageBody; } set { mMessageBody = value; SetNeedsDisplay(); } }
- private DateTime mTime;
- public DateTime Time { get { return mTime; } set { mTime = value; SetNeedsDisplay(); } }
- public string mDirection;
- public string Direction { get { return mDirection; } set { mDirection = value; SetNeedsDisplay(); } }
- #endregion
- #region Content Views
- private UILabel mDirectionLabel;
- private UILabel mMessageBodyLabel;
- private UILabel mTimeLabel;
- #endregion
- #region Initialization
- public UIConversationBubbleView(string body, DateTime time, string direction)
- {
- TranslatesAutoresizingMaskIntoConstraints = false;
- mMessageBody = body;
- mTime = time;
- mDirection = direction;
- InitializeSubviews();
- }
- private void InitializeSubviews() {
- // init message direction label
- mDirectionLabel = new UILabel();
- AddSubview(mDirectionLabel);
- mDirectionLabel.TranslatesAutoresizingMaskIntoConstraints = false;
- mDirectionLabel.TextAlignment = UITextAlignment.Left;
- mDirectionLabel.TextColor = UIColor.FromCGColor(Direction == "MO" ? MOColor : MTColor);
- mDirectionLabel.LayoutMargins = new UIEdgeInsets(0, 0, 0, 0);
- // init message body label
- mMessageBodyLabel = new UILabel();
- AddSubview(mMessageBodyLabel);
- mMessageBodyLabel.TranslatesAutoresizingMaskIntoConstraints = false;
- mMessageBodyLabel.LineBreakMode = UILineBreakMode.WordWrap;
- mMessageBodyLabel.TextColor = UIColor.Black;
- mMessageBodyLabel.Lines = 0;
- mMessageBodyLabel.LayoutMargins = new UIEdgeInsets(0, 0, 0, 0);
- // init the timestamp label
- mTimeLabel = new UILabel();
- AddSubview(mTimeLabel);
- mTimeLabel.TranslatesAutoresizingMaskIntoConstraints = false;
- mTimeLabel.TextColor = UIColor.LightGray;
- mTimeLabel.TextAlignment = UITextAlignment.Left;
- mTimeLabel.LayoutMargins = new UIEdgeInsets(0, 0, 0, 0);
- // Set the Text values
- mDirectionLabel.Text = Direction;
- mMessageBodyLabel.Text = MessageBody;
- mTimeLabel.Text = GetDateString(Time);
- AddSubviewConstraints();
- SetNeedsDisplay();
- LayoutSubviews();
- }
- private void AddSubviewConstraints() {
- var leftConstraint = (mDirection == "MO") ? BUBBLE_INDENT_MARGIN : BUBBLE_MARGIN;
- var rightConstraint = (mDirection == "MO") ? BUBBLE_MARGIN : BUBBLE_INDENT_MARGIN;
- // Parent view must stretch to contain subviews.
- //AddConstraint(NSLayoutConstraint.Create(this, NSLayoutAttribute.Top, NSLayoutRelation.Equal, mDirectionLabel, NSLayoutAttribute.Top, 1, 0));
- AddConstraint(NSLayoutConstraint.Create(this, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, mTimeLabel, NSLayoutAttribute.Bottom, 1, BUBBLE_MARGIN));
- AddConstraint(NSLayoutConstraint.Create(this, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, mDirectionLabel, NSLayoutAttribute.Width, 1, BUBBLE_WIDTH_OFFSET));
- //AddConstraint(NSLayoutConstraint.Create(this, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, mMessageBodyLabel, NSLayoutAttribute.Height, 1, BUBBLE_MARGIN * 2));
- // 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.
- AddConstraint(NSLayoutConstraint.Create(mDirectionLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1, BUBBLE_MARGIN));
- AddConstraint(NSLayoutConstraint.Create(mDirectionLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, leftConstraint));
- AddConstraint(NSLayoutConstraint.Create(mDirectionLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, rightConstraint));
- //AddConstraint(NSLayoutConstraint.Create(mDirectionLabel, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, 1, mDirectionLabel.Font.LineHeight));
- // Body label is below the direction label, and has the same width and left position.
- AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, mDirectionLabel, NSLayoutAttribute.Bottom, 1, 0));
- AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, leftConstraint));
- AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, rightConstraint));
- //AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1, -BUBBLE_WIDTH_OFFSET));
- //AddConstraint(NSLayoutConstraint.Create(mMessageBodyLabel, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, 1, mMessageBodyLabel.Font.LineHeight));
- // Time label is below the message body, and has the same width and left position.
- AddConstraint(NSLayoutConstraint.Create(mTimeLabel, NSLayoutAttribute.Top, NSLayoutRelation.Equal, mMessageBodyLabel, NSLayoutAttribute.Bottom, 1, 0));
- AddConstraint(NSLayoutConstraint.Create(mTimeLabel, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, leftConstraint));
- AddConstraint(NSLayoutConstraint.Create(mTimeLabel, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1, rightConstraint));
- //AddConstraint(NSLayoutConstraint.Create(mTimeLabel, NSLayoutAttribute.Height, NSLayoutRelation.GreaterThanOrEqual, 1, mTimeLabel.Font.LineHeight));
- }
- #endregion
- # region UIView overrides
- public override void LayoutSubviews()
- {
- mMessageBodyLabel.PreferredMaxLayoutWidth = Bounds.Size.Width - BUBBLE_WIDTH_OFFSET;
- base.LayoutSubviews();
- }
- #endregion
- #region Helpers
- private String GetDateString(DateTime date)
- { ... }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement