-
Notifications
You must be signed in to change notification settings - Fork 8
Add FontSize and Padding properties to SegmentedControl #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: yurkinh <17849938+yurkinh@users.noreply.github.com>
|
@copilot 'UISegmentedControl' does not contain a definition for 'ContentOffset' and no accessible extension method 'ContentOffset' accepting a first argument of type 'UISegmentedControl' could be found (are you missing a using directive or an assembly reference?) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds essential text styling capabilities to the SegmentedControl by introducing FontSize and Padding properties, addressing the limitation where text was previously hardcoded and lacked spacing from borders.
- Added
FontSizeproperty (double, default 14.0) for controlling segment text size - Added
Paddingproperty (Thickness, default 0) for spacing around segment content - Implemented platform-specific handlers for Android, iOS, and MacCatalyst with appropriate native mappings
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Plugin.Maui.SegmentedControl/Control/ISegmentedControl.cs | Added FontSize and Padding property declarations to the interface |
| src/Plugin.Maui.SegmentedControl/Control/SegmentedControl.cs | Defined BindableProperty for FontSize and Padding with default values |
| src/Plugin.Maui.SegmentedControl/Platforms/iOS/SegmentedControlHandler.cs | Implemented font size via UIStringAttributes.Font and padding via ContentOffset for iOS |
| src/Plugin.Maui.SegmentedControl/Platforms/MacCatalyst/SegmentedControlHandler.cs | Implemented font size via UIStringAttributes.Font and padding via ContentOffset for MacCatalyst |
| src/Plugin.Maui.SegmentedControl/Platforms/Android/SegmentedControlHandler.cs | Implemented font size via SetTextSize with sp units and padding via SetPadding with pixel conversion |
| src/Plugin.Maui.SegmentedControl/Platforms/Android/Resources/layout/RadioButton.axml | Removed hardcoded 12sp textSize to enable dynamic font sizing |
| samples/Test.SegmentedControl/MainPage.xaml | Added example usage demonstrating FontSize="16" and Padding="10,5,10,5" |
| README.md | Documented new FontSize and Padding properties with descriptions and default values |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (OperatingSystem.IsIOSVersionAtLeast(14, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(14, 0)) | ||
| { | ||
| handler.PlatformView.Layer.MasksToBounds = false; | ||
| handler.PlatformView.ContentOffset = new CoreGraphics.CGSize(padding.Left - padding.Right, padding.Top - padding.Bottom); |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContentOffset is intended to shift the content within segments, not to implement padding. Using ContentOffset with (padding.Left - padding.Right, padding.Top - padding.Bottom) will offset content in unexpected ways when padding values differ (e.g., left=10, right=5 produces an offset of 5, not traditional padding). This approach may not provide the expected padding behavior. Consider using UIEdgeInsets via contentEdgeInsets if available, or document this limitation clearly.
| if (OperatingSystem.IsIOSVersionAtLeast(14, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(14, 0)) | |
| { | |
| handler.PlatformView.Layer.MasksToBounds = false; | |
| handler.PlatformView.ContentOffset = new CoreGraphics.CGSize(padding.Left - padding.Right, padding.Top - padding.Bottom); | |
| // Use ContentEdgeInsets if available (iOS 15+), otherwise do nothing. | |
| if (OperatingSystem.IsIOSVersionAtLeast(15, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(15, 0)) | |
| { | |
| handler.PlatformView.Layer.MasksToBounds = false; | |
| handler.PlatformView.ContentEdgeInsets = new UIEdgeInsets(padding.Top, padding.Left, padding.Bottom, padding.Right); |
| if (OperatingSystem.IsIOSVersionAtLeast(14, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(14, 0)) | ||
| { | ||
| segmentControl.Layer.MasksToBounds = false; | ||
| segmentControl.ContentOffset = new CoreGraphics.CGSize(padding.Left - padding.Right, padding.Top - padding.Bottom); |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContentOffset is intended to shift the content within segments, not to implement padding. Using ContentOffset with (padding.Left - padding.Right, padding.Top - padding.Bottom) will offset content in unexpected ways when padding values differ (e.g., left=10, right=5 produces an offset of 5, not traditional padding). This approach may not provide the expected padding behavior. Consider using UIEdgeInsets via contentEdgeInsets if available, or document this limitation clearly.
| if (OperatingSystem.IsIOSVersionAtLeast(14, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(14, 0)) | |
| { | |
| segmentControl.Layer.MasksToBounds = false; | |
| segmentControl.ContentOffset = new CoreGraphics.CGSize(padding.Left - padding.Right, padding.Top - padding.Bottom); | |
| if (OperatingSystem.IsIOSVersionAtLeast(13, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(13, 0)) | |
| { | |
| segmentControl.Layer.MasksToBounds = false; | |
| segmentControl.ContentEdgeInsets = new UIEdgeInsets((nfloat)padding.Top, (nfloat)padding.Left, (nfloat)padding.Bottom, (nfloat)padding.Right); |
| if (OperatingSystem.IsIOSVersionAtLeast(14, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(14, 0)) | ||
| { | ||
| handler.PlatformView.Layer.MasksToBounds = false; | ||
| handler.PlatformView.ContentOffset = new CoreGraphics.CGSize(padding.Left - padding.Right, padding.Top - padding.Bottom); |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContentOffset is intended to shift the content within segments, not to implement padding. Using ContentOffset with (padding.Left - padding.Right, padding.Top - padding.Bottom) will offset content in unexpected ways when padding values differ (e.g., left=10, right=5 produces an offset of 5, not traditional padding). This approach may not provide the expected padding behavior. Consider using UIEdgeInsets via contentEdgeInsets if available, or document this limitation clearly.
| handler.PlatformView.ContentOffset = new CoreGraphics.CGSize(padding.Left - padding.Right, padding.Top - padding.Bottom); | |
| handler.PlatformView.ContentEdgeInsets = new UIEdgeInsets((nfloat)padding.Top, (nfloat)padding.Left, (nfloat)padding.Bottom, (nfloat)padding.Right); |
| static void MapSelectedTextColor(SegmentedControlHandler handler, SegmentedControl control) | ||
| { | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() { ForegroundColor = control.SelectedTextColor.ToPlatform() }, UIControlState.Selected); | ||
| var font = UIKit.UIFont.SystemFontOfSize((nfloat)control.FontSize); | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() | ||
| { | ||
| ForegroundColor = control.SelectedTextColor.ToPlatform(), | ||
| Font = font | ||
| }, UIControlState.Selected); | ||
| } | ||
|
|
||
| static void MapTextColor(SegmentedControlHandler handler, SegmentedControl control) | ||
| { | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() { ForegroundColor = control.TextColor.ToPlatform() }, UIControlState.Normal); | ||
| var font = UIKit.UIFont.SystemFontOfSize((nfloat)control.FontSize); | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() | ||
| { | ||
| ForegroundColor = control.TextColor.ToPlatform(), | ||
| Font = font | ||
| }, UIControlState.Normal); | ||
| } | ||
|
|
||
| static void MapFontSize(SegmentedControlHandler handler, SegmentedControl control) | ||
| { | ||
| var font = UIKit.UIFont.SystemFontOfSize((nfloat)control.FontSize); | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() | ||
| { | ||
| ForegroundColor = control.SelectedTextColor.ToPlatform(), | ||
| Font = font | ||
| }, UIControlState.Selected); | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() | ||
| { | ||
| ForegroundColor = control.TextColor.ToPlatform(), | ||
| Font = font | ||
| }, UIControlState.Normal); | ||
| } |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] There's significant code duplication between MapFontSize, MapSelectedTextColor, and MapTextColor methods. All three methods create the same font object and call SetTitleTextAttributes with nearly identical code. Consider extracting a helper method like UpdateTitleTextAttributes() that applies both color and font for all states to reduce duplication and make maintenance easier.
| static void MapSelectedTextColor(SegmentedControlHandler handler, SegmentedControl control) | ||
| { | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() { ForegroundColor = control.SelectedTextColor.ToPlatform() }, UIControlState.Selected); | ||
| var font = UIKit.UIFont.SystemFontOfSize((nfloat)control.FontSize); | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() | ||
| { | ||
| ForegroundColor = control.SelectedTextColor.ToPlatform(), | ||
| Font = font | ||
| }, UIControlState.Selected); | ||
| } | ||
|
|
||
| static void MapTextColor(SegmentedControlHandler handler, SegmentedControl control) | ||
| { | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() { ForegroundColor = control.TextColor.ToPlatform() }, UIControlState.Normal); | ||
| var font = UIKit.UIFont.SystemFontOfSize((nfloat)control.FontSize); | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() | ||
| { | ||
| ForegroundColor = control.TextColor.ToPlatform(), | ||
| Font = font | ||
| }, UIControlState.Normal); | ||
| } | ||
|
|
||
| static void MapFontSize(SegmentedControlHandler handler, SegmentedControl control) | ||
| { | ||
| var font = UIKit.UIFont.SystemFontOfSize((nfloat)control.FontSize); | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() | ||
| { | ||
| ForegroundColor = control.SelectedTextColor.ToPlatform(), | ||
| Font = font | ||
| }, UIControlState.Selected); | ||
| handler.PlatformView.SetTitleTextAttributes(new UIStringAttributes() | ||
| { | ||
| ForegroundColor = control.TextColor.ToPlatform(), | ||
| Font = font | ||
| }, UIControlState.Normal); | ||
| } |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] There's significant code duplication between MapFontSize, MapSelectedTextColor, and MapTextColor methods. All three methods create the same font object and call SetTitleTextAttributes with nearly identical code. Consider extracting a helper method like UpdateTitleTextAttributes() that applies both color and font for all states to reduce duplication and make maintenance easier.
| if (OperatingSystem.IsIOSVersionAtLeast(14, 0) || OperatingSystem.IsMacCatalystVersionAtLeast(14, 0)) | ||
| { | ||
| segmentControl.Layer.MasksToBounds = false; | ||
| segmentControl.ContentOffset = new CoreGraphics.CGSize(padding.Left - padding.Right, padding.Top - padding.Bottom); |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ContentOffset is intended to shift the content within segments, not to implement padding. Using ContentOffset with (padding.Left - padding.Right, padding.Top - padding.Bottom) will offset content in unexpected ways when padding values differ (e.g., left=10, right=5 produces an offset of 5, not traditional padding). This approach may not provide the expected padding behavior. Consider using UIEdgeInsets via contentEdgeInsets if available, or document this limitation clearly.
| segmentControl.ContentOffset = new CoreGraphics.CGSize(padding.Left - padding.Right, padding.Top - padding.Bottom); | |
| segmentControl.ContentEdgeInsets = new UIEdgeInsets((nfloat)padding.Top, (nfloat)padding.Left, (nfloat)padding.Bottom, (nfloat)padding.Right); |
…tPositionAdjustment Co-authored-by: MykhailoDav <155299608+MykhailoDav@users.noreply.github.com>
Fixed in de5c038. Replaced |
The control lacked basic text styling properties, forcing text to use hardcoded sizes and rendering with no spacing from borders.
Changes
Added properties
FontSize(double, default 14.0) - controls segment text sizePadding(Thickness, default 0) - adds spacing around segment contentAndroid
SetTextSize()with sp unitsSetPadding()with pixel conversioniOS/MacCatalyst
UIStringAttributes.Fontfor both statesSetContentPositionAdjustment()Usage
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.