xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.48k stars 514 forks source link

GetTitleTextAttributes in Microsoft.IOS return is different than in Xamarin.IOS #18195

Closed jamesingreersc closed 1 year ago

jamesingreersc commented 1 year ago

Both below methods reside in UISegmentedControl

Steps to Reproduce

  1. Attempt to convert a Xamarin.IOS project to .Net Microsoft.IOS project
  2. try converting this method from Xamarin.IOS to Microsoft.IOS .Net6 UITextAttributes routeSegmentControlAttributes = segmentSaleType.GetTitleTextAttributes(UIControlState.Selected);

Expected Behavior

Method should return a UITextAttributes object like in Xamarin.IOS

Actual Behavior

Actual return value is a UIStringAttributes object

Environment

Visual studio 2022 preview. Windows 10. Microsoft IOS .Net 6

Version information ``` ```

Build Logs

Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type 'UIKit.UIStringAttributes' to 'UIKit.UITextAttributes' DSDRouteManager.IOS

Example Project (If Possible)

rolfbjarne commented 1 year ago

Can you explain exactly why it's a problem that the GetTitleTextAttributes method returns a UIStringAttributes instead of a UITextAttributes object? UIStringAttributes should be a more accurate type.

As to your code, this should work:

var routeSegmentControlAttributes = segmentSaleType.GetTitleTextAttributes(UIControlState.Selected);
ghost commented 1 year ago

Hi @jamesingreersc. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

jamesingreersc commented 1 year ago

@rolfbjarne The biggest problem with the return type is that it breaks the existing code. UIStringAttributes doesn't contain a TextColor property. I'm just trying to convert to .Net as close as possible to the existing code base. UITextAttributes routeSegmentControlAttributes =routeSegmentControl.GetTitleTextAttributes(UIControlState.Selected); if (routeSegmentControlAttributes == null) routeSegmentControlAttributes = routeSegmentControl.GetTitleTextAttributes(UIControlState.Normal); if (routeSegmentControlAttributes == null) routeSegmentControlAttributes = new UITextAttributes() { Font = UIFont.FromName("System", 16), TextColor = UIColor.White }; routeSegmentControlAttributes.Font = UIFont.SystemFontOfSize(16, UIFontWeight.Regular); routeSegmentControlAttributes.TextColor = UIColor.White;

rolfbjarne commented 1 year ago

UIStringAttributes doesn't contain a TextColor property.

You can use the ForegroundColor property instead.

According to Apple's documentation, UITextAttributeTextColor (the underlying field for UITextAttributes.TextColor) is deprecated in favor of NSForegroundColorAttributeName instead (the underlying field for UIStringAttributes.ForegroundColor).

jamesingreersc commented 1 year ago

@rolfbjarne Awesome. Just tried that and it works great. Thanks for your help! James