Swiftui分割的采摘器动态类型
我正在尝试使用SwiftUI进行动态类型,以使用分段的选择器进行响应。我知道我必须访问旧的Uikit东西,并且使一切都起作用,除了它响应动态类型的更改(颜色,字体,尺寸等)。
正确实现这一目标非常重要,因为我们的客户可能需要扩大类型的可访问性。这是我没有动态类型的整个应用程序中唯一的元素。
另外:我需要能够设置它应该像我所读的文本修饰符(.dynamictim ... .xxlarge)时一样规模的大小,
我读过的所有内容都说您必须在uilabel(即下面的代码中的标签),但没有什么可以在uisegingedcontrol中标记的,这可能是为什么它无法正常工作的原因,但是我还没有找到一个如何正确执行此操作的示例15+。
这是代码(在.onappear中):( optionsbkg,textprimary和textsecdary是我在资产中拥有的自定义颜色)
UISegmentedControl.appearance().selectedSegmentTintColor = UIColor(Color("optionsbkg"))
if let fontMedium = UIFont(name: "HelveticaNeue-Medium", size: 14) {
let label = UILabel()
label.adjustsFontForContentSizeCategory = true
label.font = UIFontMetrics(forTextStyle: .footnote).scaledFont(for: fontMedium)
UISegmentedControl.appearance().setTitleTextAttributes(
[
.font: fontMedium,
.foregroundColor: UIColor(Color("TextSecondary"))
],
for: .normal
)
}
if let fontBold = UIFont(name: "HelveticaNeue-Bold", size: 14) {
let label = UILabel()
label.adjustsFontForContentSizeCategory = true
label.font = UIFontMetrics(forTextStyle: .footnote).scaledFont(for: fontBold)
UISegmentedControl.appearance().setTitleTextAttributes(
[
.font: fontBold,
.foregroundColor: UIColor(Color("TextPrimary"))
],
for: .selected
)
}
I'm trying to get dynamic type to respond with a segmented picker using SwiftUI. I know I have to access the old UIKit stuff and have everything working except for it responding to dynamic type changes (colors, fonts, sizes, etc. all work).
It's very important to get this right, as our clients may have accessibility needs to enlarge the type. This is the only element in the entire app I haven't got dynamic type working on.
ALSO: I need to be able to set how large it should scale like I do when I set a Text() modifier of .dynamicTypeSize(.medium ... .xxLarge)
Everything I've read says you have to set the values on UILabel (i.e. label in my code below) but nothing is ever calling to label in the UISegmentedControl which is probably why it's not working correctly, but I haven't found one example of how to do this properly for iOS 15+.
Here's the code (it's in .onAppear): (optionsbkg, TextPrimary, and TextSecondary are custom colors I have in the assets)
UISegmentedControl.appearance().selectedSegmentTintColor = UIColor(Color("optionsbkg"))
if let fontMedium = UIFont(name: "HelveticaNeue-Medium", size: 14) {
let label = UILabel()
label.adjustsFontForContentSizeCategory = true
label.font = UIFontMetrics(forTextStyle: .footnote).scaledFont(for: fontMedium)
UISegmentedControl.appearance().setTitleTextAttributes(
[
.font: fontMedium,
.foregroundColor: UIColor(Color("TextSecondary"))
],
for: .normal
)
}
if let fontBold = UIFont(name: "HelveticaNeue-Bold", size: 14) {
let label = UILabel()
label.adjustsFontForContentSizeCategory = true
label.font = UIFontMetrics(forTextStyle: .footnote).scaledFont(for: fontBold)
UISegmentedControl.appearance().setTitleTextAttributes(
[
.font: fontBold,
.foregroundColor: UIColor(Color("TextPrimary"))
],
for: .selected
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们遇到了相同的问题,并使用这样的动态字体解决了它:
我们还通过内省使用了基础UIKIT代码。
此处的关键部分是使用
.subheadline
之类的字体,以便它基于DynamiCfonts缩放。现在出现了一个格外的部分,因此它看起来不会破裂,因为选择器不会随字体扩展。
我们添加了一个具有键入性的框架修饰符,以便基于动态型设置高度:
这是扩展:
对我们来说,它可以完美地工作
We had the same problem and solved it with dynamic fonts like this:
We also used the underlying UIKit code via introspect
The key part here is to use a font like
.subheadline
so that it scales based on DynamicFonts.And now comes the extra part so that it doesn't look broken because the picker doesn't scale with the fonts.
We added a frame modifier that has a typeExtension, so that it sets the height based on DynamicTypes:
And here is the extension:
For us it worked flawlessly
我真的希望苹果已经弄清楚了,但是由于我们仍然求助于旧的Uikit代码来实现它,并且似乎没有任何有关如何做的文档,所以我认为我会发布解决方案。
仅使用text()元素创建分段的选择器,然后将UIKIT代码放入.onappear中。您可以将此精确的代码复制到Xcode中的SwiftUi文件中,并且可以使用。您唯一需要记住的是将我的自定义颜色更改为项目中想要的任何颜色。
注意:这将动态文本大小限制为XXL的最大值,这是我需要的。如果您不希望对文本可以扩展尺寸,或者将值更改为希望放置限制的大小,则可以随意删除三元。
有一些问题需要注意:
您无法使用Xcode的环境覆盖以设置动态文本大小并看到其实时变化。它会更改,但是只有当您使用Xcode的环境覆盖并设置动态文本大小时,请转到另一个视图,然后返回此视图。然后,您将看到更改。
类型大小只有在您访问此特定页面时才会更改。您无法在ContentView中在全球设置它,并期望每当您更改它将自动更新的类型大小时。它根本不起作用。
我最终要做的是使用包含所有UIKIT代码的静态函数以及类型DynamiTyceSypesize的参数创建一个类构造,并在.onappear中调用该功能。至少可以确保如果用户更改类型大小,当他们使用分段的选择器访问另一个视图时,该类型将反映他们的选择。
如果有人对如何完成此操作有更好的了解(使用实时更新?),请发布答复。
I truly wish Apple would figure this out already, but since we still have resort to old UIKit code to make it happen, and there doesn't seem to be any documentation on how to do it, I thought I would post the solution.
Create your segmented picker using only Text() elements, and place the UIKit code in .onAppear. You can copy/paste this exact code into a SwiftUI file in XCode and it will work. The only thing you need to remember is to change my custom colors to whatever colors you want in your project.
Note: This limits the dynamic text size to a max of xxL, which is what I needed. Feel free to remove the ternary if you don't wish to place a limit on large the text can scale, or change the value to the size of which you wish to place a limit.
There are a few issues to be aware of:
You cannot check this by using XCode's Environment Overrides to set the dynamic text size and see it change in real-time. It will change, but only if you use XCode's Environment Overrides and set the dynamic text size, go to another view, and then return to this view. You will then see the changes.
The type size will only change when you visit this specific page. You cannot set it globally in ContentView and expect that whenever you change the type size it will automatically update. It simply doesn't work.
What I ended up doing was creating a class with a static func that contained all of the UIKit code, along with a parameter of type DynamicTypeSize so that on every view a segmented picker is present, I put the @Environment variable in the struct, and call to that function in .onAppear. That at least ensures that if the user changes the type size, when they visit another view with a segmented picker, the type will reflect their choice.
If anyone has a better idea of how to accomplish this (with real-time updates???) please post a response.