private void AddBackgroundAndBorderLayer(CGRect fullRect) { var backgroundLayer = new DisposableCALayer { Frame = fullRect }; InsertScrollViewSubLayer(backgroundLayer, 0); var borderLayer = new DisposableCALayer { BackgroundColor = BorderColor.CGColor }; switch (borderType) { case HMSegmentedControlBorderType.Top: borderLayer.Frame = new CGRect(0, 0, fullRect.Size.Width, borderWidth); break; case HMSegmentedControlBorderType.Left: borderLayer.Frame = new CGRect(0, 0, borderWidth, fullRect.Size.Height); break; case HMSegmentedControlBorderType.Bottom: borderLayer.Frame = new CGRect(0, fullRect.Size.Height - borderWidth, fullRect.Size.Width, borderWidth); break; case HMSegmentedControlBorderType.Right: borderLayer.Frame = new CGRect(fullRect.Size.Width - borderWidth, 0, borderWidth, fullRect.Size.Height); break; } AddScrollViewSubLayer(borderLayer); }
private void Initialize() { scrollView = new HMScrollView { ScrollsToTop = false, ShowsVerticalScrollIndicator = false, ShowsHorizontalScrollIndicator = false }; AddSubview(scrollView); Opaque = false; SelectionIndicatorColor = UIColor.FromRGBA(52.0f / 255.0f, 181.0f / 255.0f, 229.0f / 255.0f, 1.0f); SelectedIndex = 0; SegmentEdgeInset = new UIEdgeInsets(0, 5, 0, 5); SelectionIndicatorHeight = 5.0f; selectionIndicatorEdgeInsets = new UIEdgeInsets(0, 0, 0, 0); SelectionStyle = HMSegmentedControlSelectionStyle.TextWidthStripe; SelectionIndicatorLocation = HMSegmentedControlIndicatorLocation.Up; segmentWidthStyle = HMSegmentedControlWidthStyle.Fixed; UserDraggable = true; TouchEnabled = true; VerticalDividerEnabled = false; VerticalDividerColor = UIColor.Black; BorderColor = UIColor.Black; borderWidth = 1.0f; ShouldAnimateUserSelection = true; selectionIndicatorBoxOpacity = 0.2f; SelectionIndicatorArrowLayer = new DisposableCALayer(); SelectionIndicatorStripLayer = new DisposableCALayer(); SelectionIndicatorBoxLayer = new DisposableCALayer { Opacity = selectionIndicatorBoxOpacity, BorderWidth = 1.0f }; ContentMode = UIViewContentMode.Redraw; }
public override void Draw(CGRect rect) { if (BackgroundColor != null) { BackgroundColor.SetFill(); } SelectionIndicatorArrowLayer.BackgroundColor = SelectionIndicatorColor.CGColor; SelectionIndicatorStripLayer.BackgroundColor = SelectionIndicatorColor.CGColor; SelectionIndicatorBoxLayer.BackgroundColor = SelectionIndicatorColor.CGColor; SelectionIndicatorBoxLayer.BorderColor = SelectionIndicatorColor.CGColor; scrollView.Layer.Sublayers = new DisposableCALayer[0]; ClearScrollViewSubLayers(); var oldRect = rect; if (type == HMSegmentedControlType.Text) { for (int idx = 0; idx < sectionTitles.Count; idx++) { var size = MeasureTitle(idx); var stringWidth = size.Width; var stringHeight = size.Height; CGRect rectangle, rectDiv, rectFull; var locationUp = SelectionIndicatorLocation == HMSegmentedControlIndicatorLocation.Up; var selectionStyleNotBox = SelectionStyle != HMSegmentedControlSelectionStyle.Box; var y = (float)Math.Round(((Frame.Height - (selectionStyleNotBox ? 1 : 0 * SelectionIndicatorHeight)) / 2) - (stringHeight / 2) + (SelectionIndicatorHeight * (locationUp ? 1 : 0))); if (segmentWidthStyle == HMSegmentedControlWidthStyle.Fixed) { rectangle = new CGRect((segmentWidth * idx) + (segmentWidth - stringWidth) / 2, y, stringWidth, stringHeight); rectDiv = new CGRect((segmentWidth * idx) + (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4)); rectFull = new CGRect(segmentWidth * idx, 0, segmentWidth, oldRect.Size.Height); } else { var xOffset = 0.0f; var i = 0; foreach (var width in segmentWidths) { if (idx == i) { break; } xOffset += width; i++; } var widthForIndex = segmentWidths[idx]; rectangle = new CGRect(xOffset, y, widthForIndex, stringHeight); rectFull = new CGRect(segmentWidth * idx, 0, widthForIndex, oldRect.Size.Height); rectDiv = new CGRect(xOffset - (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4)); } rectangle = new CGRect((float)Math.Ceiling(rectangle.X), (float)Math.Ceiling(rectangle.Y), (float)Math.Ceiling(rectangle.Size.Width), (float)Math.Ceiling(rectangle.Size.Height)); var titleLayer = new DisposableCATextLayer { Frame = rectangle, AlignmentMode = CATextLayer.AlignmentCenter, TruncationMode = CATextLayer.TruncantionEnd, AttributedString = AttributedTitle(idx), ContentsScale = UIScreen.MainScreen.Scale }; AddScrollViewSubLayer(titleLayer); if (VerticalDividerEnabled) { var verticalDividerLayer = new DisposableCALayer { Frame = rectDiv, BackgroundColor = VerticalDividerColor.CGColor }; AddScrollViewSubLayer(verticalDividerLayer); } AddBackgroundAndBorderLayer(rectFull); } } else if (type == HMSegmentedControlType.Image) { for (int idx = 0; idx < sectionTitles.Count; idx++) { var icon = sectionImages[idx]; var imageWidth = icon.Size.Width; var imageHeight = icon.Size.Height; var y = (float)Math.Round(Frame.Height - SelectionIndicatorHeight) / 2 - imageHeight / 2 + (SelectionIndicatorLocation == HMSegmentedControlIndicatorLocation.Up ? SelectionIndicatorHeight : 0); var x = segmentWidth * idx + (segmentWidth - imageWidth) / 2.0f; var rectNew = new CGRect(x, y, imageWidth, imageHeight); var imageLayer = new DisposableCALayer { Frame = rectNew }; if (SelectedIndex == idx && sectionSelectedImages != null) { imageLayer.Contents = sectionSelectedImages[idx].CGImage; } else { imageLayer.Contents = icon.CGImage; } AddScrollViewSubLayer(imageLayer); if (VerticalDividerEnabled && idx > 0) { var verticalDividerLayer = new DisposableCALayer { Frame = new CGRect((segmentWidth * idx) - (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4)), BackgroundColor = VerticalDividerColor.CGColor }; AddScrollViewSubLayer(verticalDividerLayer); } AddBackgroundAndBorderLayer(rectNew); } } else if (type == HMSegmentedControlType.TextAndImage) { for (int idx = 0; idx < sectionTitles.Count; idx++) { var icon = sectionImages[idx]; var imageWidth = icon.Size.Width; var imageHeight = icon.Size.Height; var stringHeight = MeasureTitle(idx).Height; var yOffset = (float)Math.Round(((Frame.Height - SelectionIndicatorHeight) / 2) - (stringHeight / 2)); var imageXOffset = SegmentEdgeInset.Left; var textXOffset = SegmentEdgeInset.Left; var textWidth = (nfloat)0.0f; if (segmentWidthStyle == HMSegmentedControlWidthStyle.Fixed) { imageXOffset = (segmentWidth * idx) + (segmentWidth / 2.0f) - (imageWidth / 2.0f); textXOffset = segmentWidth * idx; textWidth = segmentWidth; } else { var xOffset = 0.0f; var i = 0; foreach (var width in segmentWidths) { if (idx == i) { break; } xOffset += width; i++; } imageXOffset = xOffset + (segmentWidths[idx] / 2.0f) - (imageWidth / 2.0f); textXOffset = xOffset; textWidth = segmentWidths[idx]; } var imageYOffset = (float)Math.Round((Frame.Height - SelectionIndicatorHeight) / 2.0f); var imageRect = new CGRect(imageXOffset, imageYOffset, imageWidth, imageHeight); var textRect = new CGRect((float)Math.Ceiling(textXOffset), (float)Math.Ceiling(yOffset), (float)Math.Ceiling(textWidth), (float)Math.Ceiling(stringHeight)); var titleLayer = new DisposableCATextLayer { Frame = textRect, AlignmentMode = CATextLayer.AlignmentCenter, AttributedString = AttributedTitle(idx), TruncationMode = CATextLayer.TruncantionEnd, ContentsScale = UIScreen.MainScreen.Scale }; var imageLayer = new DisposableCALayer { Frame = imageRect, Contents = (SelectedIndex == idx && sectionSelectedImages != null) ? sectionSelectedImages[idx].CGImage : icon.CGImage }; AddScrollViewSubLayer(imageLayer); AddScrollViewSubLayer(titleLayer); AddBackgroundAndBorderLayer(imageRect); } } if (SelectedIndex != -1) { if (SelectionStyle == HMSegmentedControlSelectionStyle.Arrow) { SetArrowFrame(); AddScrollViewSubLayer(SelectionIndicatorArrowLayer); } else { if (SelectionIndicatorStripLayer.SuperLayer == null) { SelectionIndicatorStripLayer.Frame = FrameForSelectionIndicator(); AddScrollViewSubLayer(SelectionIndicatorStripLayer); if (SelectionStyle == HMSegmentedControlSelectionStyle.Box && SelectionIndicatorBoxLayer.SuperLayer == null) { SelectionIndicatorBoxLayer.Frame = FrameForFillerSelectionIndicator(); InsertScrollViewSubLayer(SelectionIndicatorBoxLayer, 0); } } } } }
private void AddBackgroundAndBorderLayer(RectangleF fullRect) { var backgroundLayer = new DisposableCALayer { Frame = fullRect }; InsertScrollViewSubLayer(backgroundLayer, 0); var borderLayer = new DisposableCALayer { BackgroundColor = BorderColor.CGColor }; switch (borderType) { case HMSegmentedControlBorderType.Top: borderLayer.Frame = new RectangleF(0, 0, fullRect.Size.Width, borderWidth); break; case HMSegmentedControlBorderType.Left: borderLayer.Frame = new RectangleF(0, 0, borderWidth, fullRect.Size.Height); break; case HMSegmentedControlBorderType.Bottom: borderLayer.Frame = new RectangleF(0, fullRect.Size.Height - borderWidth, fullRect.Size.Width, borderWidth); break; case HMSegmentedControlBorderType.Right: borderLayer.Frame = new RectangleF(fullRect.Size.Width - borderWidth, 0, borderWidth, fullRect.Size.Height); break; } AddScrollViewSubLayer(borderLayer); }
public override void Draw(RectangleF rectangle) { if (BackgroundColor != null) BackgroundColor.SetFill(); SelectionIndicatorArrowLayer.BackgroundColor = SelectionIndicatorColor.CGColor; SelectionIndicatorStripLayer.BackgroundColor = SelectionIndicatorColor.CGColor; SelectionIndicatorBoxLayer.BackgroundColor = SelectionIndicatorColor.CGColor; SelectionIndicatorBoxLayer.BorderColor = SelectionIndicatorColor.CGColor; scrollView.Layer.Sublayers = new DisposableCALayer[0]; ClearScrollViewSubLayers(); var oldRect = rectangle; if (type == HMSegmentedControlType.Text) { for (int idx = 0; idx < sectionTitles.Count; idx++) { var size = MeasureTitle(idx); var stringWidth = size.Width; var stringHeight = size.Height; RectangleF rect, rectDiv, rectFull; var locationUp = SelectionIndicatorLocation == HMSegmentedControlIndicatorLocation.Up; var selectionStyleNotBox = SelectionStyle != HMSegmentedControlSelectionStyle.Box; var y = (float)Math.Round(((this.Frame.Height - (selectionStyleNotBox ? 1 : 0 * SelectionIndicatorHeight)) / 2) - (stringHeight / 2) + (SelectionIndicatorHeight * (locationUp ? 1 : 0))); if (segmentWidthStyle == HMSegmentedControlWidthStyle.Fixed) { rect = new RectangleF((segmentWidth * idx) + (segmentWidth - stringWidth) / 2, y, stringWidth, stringHeight); rectDiv = new RectangleF((segmentWidth * idx) + (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4)); rectFull = new RectangleF(segmentWidth * idx, 0, segmentWidth, oldRect.Size.Height); } else { var xOffset = 0.0f; var i = 0; foreach (var width in segmentWidths) { if (idx == i) break; xOffset += width; i++; } var widthForIndex = segmentWidths[idx]; rect = new RectangleF(xOffset, y, widthForIndex, stringHeight); rectFull = new RectangleF(segmentWidth * idx, 0, widthForIndex, oldRect.Size.Height); rectDiv = new RectangleF(xOffset - (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4)); } rect = new RectangleF((float)Math.Ceiling(rect.X), (float)Math.Ceiling(rect.Y), (float)Math.Ceiling(rect.Size.Width), (float)Math.Ceiling(rect.Size.Height)); var titleLayer = new DisposableCATextLayer { Frame = rect, AlignmentMode = CATextLayer.AlignmentCenter, TruncationMode = CATextLayer.TruncantionEnd, AttributedString = AttributedTitle(idx), ContentsScale = UIScreen.MainScreen.Scale, }; AddScrollViewSubLayer(titleLayer); if (VerticalDividerEnabled) { var verticalDividerLayer = new DisposableCALayer { Frame = rectDiv, BackgroundColor = VerticalDividerColor.CGColor }; AddScrollViewSubLayer(verticalDividerLayer); } AddBackgroundAndBorderLayer(rectFull); } } else if (type == HMSegmentedControlType.Image) { for (int idx = 0; idx < sectionTitles.Count; idx++) { var icon = sectionImages[idx]; var imageWidth = icon.Size.Width; var imageHeight = icon.Size.Height; var y = (float)Math.Round(Frame.Height - SelectionIndicatorHeight) / 2 - imageHeight / 2 + (SelectionIndicatorLocation == HMSegmentedControlIndicatorLocation.Up ? SelectionIndicatorHeight : 0); var x = segmentWidth * idx + (segmentWidth - imageWidth) / 2.0f; var rectNew = new RectangleF(x, y, imageWidth, imageHeight); var imageLayer = new DisposableCALayer { Frame = rectNew }; if (SelectedIndex == idx && sectionSelectedImages != null) imageLayer.Contents = sectionSelectedImages[idx].CGImage; else imageLayer.Contents = icon.CGImage; AddScrollViewSubLayer(imageLayer); if (VerticalDividerEnabled && idx > 0) { var verticalDividerLayer = new DisposableCALayer { Frame = new RectangleF((segmentWidth * idx) - (VerticalDividerWidth / 2), SelectionIndicatorHeight * 2, VerticalDividerWidth, Frame.Size.Height - (SelectionIndicatorHeight * 4)), BackgroundColor = VerticalDividerColor.CGColor }; AddScrollViewSubLayer(verticalDividerLayer); } AddBackgroundAndBorderLayer(rectNew); } } else if (type == HMSegmentedControlType.TextAndImage) { for (int idx = 0; idx < sectionTitles.Count; idx++) { var icon = sectionImages[idx]; var imageWidth = icon.Size.Width; var imageHeight = icon.Size.Height; var stringHeight = MeasureTitle(idx).Height; var yOffset = (float)Math.Round(((Frame.Height - SelectionIndicatorHeight) / 2) - (stringHeight / 2)); var imageXOffset = SegmentEdgeInset.Left; var textXOffset = SegmentEdgeInset.Left; var textWidth = 0.0f; if (segmentWidthStyle == HMSegmentedControlWidthStyle.Fixed) { imageXOffset = (segmentWidth * idx) + (segmentWidth / 2.0f) - (imageWidth / 2.0f); textXOffset = segmentWidth * idx; textWidth = segmentWidth; } else { var xOffset = 0.0f; var i = 0; foreach (var width in segmentWidths) { if (idx == i) break; xOffset += width; i++; } imageXOffset = xOffset + (segmentWidths[idx] / 2.0f) - (imageWidth / 2.0f); textXOffset = xOffset; textWidth = segmentWidths[idx]; } var imageYOffset = (float)Math.Round((Frame.Height - SelectionIndicatorHeight) / 2.0f); var imageRect = new RectangleF(imageXOffset, imageYOffset, imageWidth, imageHeight); var textRect = new RectangleF((float)Math.Ceiling(textXOffset), (float)Math.Ceiling(yOffset), (float)Math.Ceiling(textWidth), (float)Math.Ceiling(stringHeight)); var titleLayer = new DisposableCATextLayer { Frame = textRect, AlignmentMode = CATextLayer.AlignmentCenter, AttributedString = AttributedTitle(idx), TruncationMode = CATextLayer.TruncantionEnd, ContentsScale = UIScreen.MainScreen.Scale }; var imageLayer = new DisposableCALayer { Frame = imageRect, Contents = (SelectedIndex == idx && sectionSelectedImages != null) ? sectionSelectedImages[idx].CGImage : icon.CGImage }; AddScrollViewSubLayer(imageLayer); AddScrollViewSubLayer(titleLayer); AddBackgroundAndBorderLayer(imageRect); } } if (SelectedIndex != -1) { if (SelectionStyle == HMSegmentedControlSelectionStyle.Arrow) { SetArrowFrame(); AddScrollViewSubLayer(SelectionIndicatorArrowLayer); } else { if (SelectionIndicatorStripLayer.SuperLayer == null) { SelectionIndicatorStripLayer.Frame = FrameForSelectionIndicator(); AddScrollViewSubLayer(SelectionIndicatorStripLayer); if (SelectionStyle == HMSegmentedControlSelectionStyle.Box && SelectionIndicatorBoxLayer.SuperLayer == null) { SelectionIndicatorBoxLayer.Frame = FrameForFillerSelectionIndicator(); InsertScrollViewSubLayer(SelectionIndicatorBoxLayer, 0); } } } } }