gpt4 book ai didi

Xamarin.iOS UIApperance 设置默认文本属性

转载 作者:行者123 更新时间:2023-12-01 12:15:50 26 4
gpt4 key购买 nike

我正在尝试弄清楚如何在 Xamarin 中实现以下代码:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}];

但是我找不到在 UIApperance 类上设置 DefaultTextAttributes 的方法。

最佳答案

缺少数量 UIAppearance Xamarin.iOS 中的功能以及关于您的问题,缺少 API。

  • 这是一个错误,🍣 我自己写的 UIAppearance.cs添加缺失的功能并更正缺失的 API 并假设没有其他 Xamarin.iOS 编码器真正使用较新的 UIAppearance自 Xamarin 中的 iOS 9 以来已损坏的功能。

首先,appearanceWhenContainedIn已弃用,您应该使用 appearanceWhenContainedInInstancesOfClasses相反。

AppearanceWhenContainedIn - was deprecated in iOS 9 and is not recommended for use.

第二,appearanceWhenContainedInInstancesOfClassesXamarin.iOS 中被错误地定义作为 仅在 tvOS 中可用 但事实并非如此。

#if TVOS
// new in iOS9 but the only option for tvOS
const string selAppearanceWhenContainedInInstancesOfClasses = "appearanceWhenContainedInInstancesOfClasses:";
~~~

UIAppearance.cs#L77

因此它不能通过 Xamarin.iOS 包装器 API 使用,但当然可以直接从 ObjC 运行时使用:

var NSForegroundColorAttributeName = Dlfcn.GetStringConstant(UIKitLibraryHandle, "NSForegroundColorAttributeName");
var defaultAttributes = NSDictionary.FromObjectsAndKeys(new NSObject[] { UIColor.Red }, new NSObject[] { NSForegroundColorAttributeName });
var styleHandle = GetAppearanceEx(Class.GetHandle("UITextField"), typeof(UISearchBar));
void_objc_msgSend_IntPtr(styleHandle, Selector.GetHandle("setDefaultTextAttributes:"), defaultAttributes.Handle);

enter image description here

下一个问题是上述代码运行所需的许多标记为内部的 Xamarin.iOS 方法,因此需要对某些源进行一些复制/粘贴/修改:

public const string selAppearanceWhenContainedInInstancesOfClasses = "appearanceWhenContainedInInstancesOfClasses:";

public static readonly IntPtr UIKitLibraryHandle = Dlfcn.dlopen("/System/Library/Frameworks/UIKit.framework/UIKit", 0);

[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
public static extern IntPtr IntPtr_objc_msgSend_IntPtr(IntPtr receiver, IntPtr selector, IntPtr arg1);

[DllImport("/usr/lib/libobjc.dylib", EntryPoint = "objc_msgSend")]
public static extern void void_objc_msgSend_IntPtr(IntPtr receiver, IntPtr selector, IntPtr arg1);

public static IntPtr GetAppearanceEx(IntPtr class_ptr, params Type[] whenFoundIn)
{
var ptrs = TypesToPointers(whenFoundIn);
var handles = NSArray.FromIntPtrs(ptrs);
using (var array = handles)
{
return IntPtr_objc_msgSend_IntPtr(class_ptr, Selector.GetHandle(selAppearanceWhenContainedInInstancesOfClasses), array.Handle);
}
}

public static IntPtr[] TypesToPointers(Type[] whenFoundIn)
{
IntPtr[] ptrs = new IntPtr[whenFoundIn.Length];

for (int i = 0; i < whenFoundIn.Length; i++)
{
if (whenFoundIn[i] == null)
throw new ArgumentException(String.Format("Parameter {0} was null, must specify a valid type", i));
if (!typeof(NSObject).IsAssignableFrom(whenFoundIn[i]))
throw new ArgumentException(String.Format("Type {0} does not derive from NSObject", whenFoundIn[i]));

var classHandle = Class.GetHandle(whenFoundIn[i]);
if (classHandle == IntPtr.Zero)
throw new ArgumentException(string.Format("Could not find the Objective-C class for {0}", whenFoundIn[i].FullName));
ptrs[i] = classHandle;
}
return ptrs;
}

关于Xamarin.iOS UIApperance 设置默认文本属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48262588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com