Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Demo/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
xmlns:controls="clr-namespace:SegmentedControl.FormsPlugin.Abstractions;assembly=SegmentedControl.FormsPlugin.Abstractions"
xmlns:local="clr-namespace:Demo;assembly=Demo">

<Application.Resources>
<ResourceDictionary>
<OnPlatform x:Key="Font" x:TypeArguments="x:String">
<On Platform="iOS" Value="SFUIText-Bold"/>
<On Platform="Android" Value="sans-serif-thin"/>
</OnPlatform>
</ResourceDictionary>
</Application.Resources>

<StackLayout x:Name="segContainer"
Padding="12"
Spacing="12">
<controls:SegmentedControl x:Name="SegControl" ValueChanged="Handle_ValueChanged">
<controls:SegmentedControl x:Name="SegControl" ValueChanged="Handle_ValueChanged" FontFamily="{StaticResource Font}" FontSize="16">
<controls:SegmentedControl.Children>
<controls:SegmentedControlOption Text="Items" />
<controls:SegmentedControlOption Text="Notes" />
Expand Down
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ xmlns:controls="clr-namespace:SegmentedControl.FormsPlugin.Abstractions;assembly
```

```xml
<controls:SegmentedControl x:Name="SegControl" TintColor="#007AFF" SelectedSegment="0">
<controls:SegmentedControl x:Name="SegControl" TintColor="#007AFF" SelectedSegment="0" FontFamily="Courier" FontSize="16">
<controls:SegmentedControl.Children>
<controls:SegmentedControlOption Text="Tab 1" />
<controls:SegmentedControlOption Text="Tab 2" />
Expand All @@ -42,11 +42,11 @@ xmlns:controls="clr-namespace:SegmentedControl.FormsPlugin.Abstractions;assembly
#### Event handler

```
public void Handle_ValueChanged(object o, int e)
public void Handle_ValueChanged(object o, SegmentedControl.FormsPlugin.Abstractions.ValueChangedEventArgs e)
{
SegContent.Children.Clear();

switch (e)
switch (e.NewValue)
{
case 0:
SegContent.Children.Add(new Label() { Text="Tab 1 selected" });
Expand All @@ -72,16 +72,23 @@ public void Handle_ValueChanged(object o, int e)

```SelectedSegment```: Selected segment index (int, default 0).

```FontFamily```: Font to use for text (string, default Helvetica).

```FontSize```: Font size to use for text (float, default 15f).

> FontFamily and FontSize supported on iOS and Android only

**Event Handlers**

```ValueChanged```: Called when a segment is selected.

#### Roadmap

* Change font family/size

#### Release Notes

2.0.2

[Android and iOS] Support for setting font and font size

2.0.1

[Android] Segmented Control Not Displaying on Second Page #71
Expand Down
Original file line number Diff line number Diff line change
@@ -1,80 +1,98 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;

namespace SegmentedControl.FormsPlugin.Abstractions
{
/// <summary>
/// SegmentedControl Interface
/// </summary>
public class SegmentedControl : View, IViewContainer<SegmentedControlOption>
{
public IList<SegmentedControlOption> Children { get; set; }

public SegmentedControl()
{
Children = new List<SegmentedControlOption>();
}

public static readonly BindableProperty TintColorProperty = BindableProperty.Create("TintColor", typeof(Color), typeof(SegmentedControl), Color.Blue);

public Color TintColor
{
get { return (Color)GetValue(TintColorProperty); }
set { SetValue(TintColorProperty, value); }
}

public static readonly BindableProperty DisabledColorProperty = BindableProperty.Create("DisabledColor", typeof(Color), typeof(SegmentedControl), Color.Gray);

public Color DisabledColor
{
get { return (Color)GetValue(DisabledColorProperty); }
set { SetValue(DisabledColorProperty, value); }
using Xamarin.Forms;

namespace SegmentedControl.FormsPlugin.Abstractions
{
/// <summary>
/// SegmentedControl Interface
/// </summary>
public class SegmentedControl : View, IViewContainer<SegmentedControlOption>
{
public IList<SegmentedControlOption> Children { get; set; }

public SegmentedControl()
{
Children = new List<SegmentedControlOption>();
}

public static readonly BindableProperty TintColorProperty = BindableProperty.Create("TintColor", typeof(Color), typeof(SegmentedControl), Color.Blue);

public Color TintColor
{
get { return (Color)GetValue(TintColorProperty); }
set { SetValue(TintColorProperty, value); }
}

public static readonly BindableProperty DisabledColorProperty = BindableProperty.Create("DisabledColor", typeof(Color), typeof(SegmentedControl), Color.Gray);

public Color DisabledColor
{
get { return (Color)GetValue(DisabledColorProperty); }
set { SetValue(DisabledColorProperty, value); }
}

public static readonly BindableProperty SelectedTextColorProperty = BindableProperty.Create("SelectedTextColor", typeof(Color), typeof(SegmentedControl), Color.White);

public Color SelectedTextColor
{
get { return (Color)GetValue(SelectedTextColorProperty); }
set { SetValue(SelectedTextColorProperty, value); }
}

public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(nameof(FontFamily), typeof(string), typeof(SegmentedControl), "Helvetica");

public string FontFamily
{
get { return (string)GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}

public static readonly BindableProperty FontSizeProperty = BindableProperty.Create(nameof(FontSize), typeof(float), typeof(SegmentedControl), 15f);

public float FontSize
{
get { return (float)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}

public static readonly BindableProperty SelectedSegmentProperty = BindableProperty.Create("SelectedSegment", typeof(int), typeof(SegmentedControl), 0);

public int SelectedSegment
{
get
{
return (int)GetValue(SelectedSegmentProperty);
}
set
{
SetValue(SelectedSegmentProperty, value);
}
}

public static readonly BindableProperty SelectedTextColorProperty = BindableProperty.Create("SelectedTextColor", typeof(Color), typeof(SegmentedControl), Color.White);

public Color SelectedTextColor
{
get { return (Color)GetValue(SelectedTextColorProperty); }
set { SetValue(SelectedTextColorProperty, value); }
}

public static readonly BindableProperty SelectedSegmentProperty = BindableProperty.Create("SelectedSegment", typeof(int), typeof(SegmentedControl), 0);

public int SelectedSegment
{
get {
return (int)GetValue(SelectedSegmentProperty);
}
set {
SetValue(SelectedSegmentProperty, value);
}
}

public event EventHandler<ValueChangedEventArgs> ValueChanged;

[EditorBrowsable(EditorBrowsableState.Never)]
public void SendValueChanged()
{
public event EventHandler<ValueChangedEventArgs> ValueChanged;

[EditorBrowsable(EditorBrowsableState.Never)]
public void SendValueChanged()
{
ValueChanged?.Invoke(this, new ValueChangedEventArgs { NewValue = this.SelectedSegment });
}
}
public class SegmentedControlOption : View
{
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(SegmentedControlOption), string.Empty);
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}

public class ValueChangedEventArgs : EventArgs
{
public int NewValue { get; set; }
}
}
}
}

public class SegmentedControlOption : View
{
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(SegmentedControlOption), string.Empty);

public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}

public class ValueChangedEventArgs : EventArgs
{
public int NewValue { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using Android.Views;
using Android.Graphics.Drawables;
using Android.Content;
using Android.Graphics;
using Android.Util;

[assembly: ExportRenderer(typeof(SegmentedControl.FormsPlugin.Abstractions.SegmentedControl), typeof(SegmentedControlRenderer))]
namespace SegmentedControl.FormsPlugin.Android
Expand Down Expand Up @@ -92,7 +94,7 @@ protected override void OnElementPropertyChanged(object sender, System.Component
case "Renderer":
Element?.SendValueChanged();
break;
case "SelectedSegment":
case nameof(Element.SelectedSegment):
var option = (RadioButton)nativeControl.GetChildAt(Element.SelectedSegment);

if (option != null)
Expand Down Expand Up @@ -129,13 +131,13 @@ protected override void OnElementPropertyChanged(object sender, System.Component

Element.SendValueChanged();
break;
case "TintColor":
case nameof(Element.TintColor):
case nameof(Element.IsEnabled):
case nameof(Element.FontFamily):
case nameof(Element.FontSize):
OnPropertyChanged();
break;
case "IsEnabled":
OnPropertyChanged();
break;
case "SelectedTextColor":
break;
case nameof(Element.SelectedTextColor):
var v = (RadioButton)nativeControl.GetChildAt(Element.SelectedSegment);
v.SetTextColor(Element.SelectedTextColor.ToAndroid());
break;
Expand Down Expand Up @@ -185,7 +187,11 @@ void ConfigureRadioButton(int i, RadioButton rb)
selectedShape.SetColor(color);
unselectedShape.SetStroke(3, color);

rb.Enabled = Element.IsEnabled;
rb.Enabled = Element.IsEnabled;

var font = Font.OfSize(Element.FontFamily, Element.FontSize);
rb.SetTypeface(font.ToTypeface(), TypefaceStyle.Normal);
rb.SetTextSize(ComplexUnitType.Sp, font.ToScaledPixel());
}

void NativeControl_ValueChanged(object sender, RadioGroup.CheckedChangeEventArgs e)
Expand Down Expand Up @@ -222,8 +228,8 @@ protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
catch (Exception ex)
{
catch
{
return;
}
}
Expand All @@ -234,4 +240,5 @@ public static void Init() {
var temp = DateTime.Now;
}
}

}
Loading