如何在swiftui中使用选择器更改文本大小

Posted

技术标签:

【中文标题】如何在swiftui中使用选择器更改文本大小【英文标题】:How to change text size using picker in swiftui 【发布时间】:2021-08-15 03:13:57 【问题描述】:

我是 swiftui 的初学者。我开始一个圣经应用程序作为初学者的项目。我创建了一个设置,并从该设置中,我想通过使用选择器来更改我的字体大小。我在网上找了4天的解决办法,还是不知道该怎么办。

问题 1:我不知道如何根据选取器选择更新字体大小。有人说要使用.onChange,但我不知道该怎么做。所以,我尝试使用 switch case 方法,但仍然不知道如何更新文本。另外,我尝试使用自定义修饰符来替换 .font(.footnote) 之类的东西。但是,我仍然不知道如何更新它,EnvironmentalObject 根本没有帮助。请指导我如何解决问题。

问题 2:每次我玩画布时,选择器选择总是从默认索引开始,这是有意义的。但是在实际的应用程序中,是否总是在用户选择不同的索引后以默认索引开始,或者我应该怎么做才能保持预选的索引。

这是我的代码:

import SwiftUI

struct Setting: View 
    @State private var fontSizeIndex = 3
    @State private var fontIndex = 1


   var body: some View 
        NavigationView 
            ScrollView 

               // Text Settings
                Form 
                    
                    // Font Size Picker
                    Section(header: Text("Text Settings")) 

                         Picker(selection: $fontSizeIndex, label: Text("Font Size"), 
                            content: 
                            Text("Extra Small").tag(1)
                            Text("Small").tag(2)
                            Text("Medium").tag(3)
                            Text("Large").tag(4)
                            Text("Extra Large").tag(5)
                         )


                        // Font Picker
                        // I plan to make custom fonts if I know how to update my 
                        // selection
                        Picker(selection: $fontIndex, label: Text("Font"), content: 
                            Text("Font1").tag(1)
                            Text("Font2").tag(2)
                            Text("Font3").tag(3)
                            Text("Font4").tag(4)
                            Text("Font5").tag(5)
                            Text("Font6").tag(6)
                            Text("Font7").tag(7)
                        )

                    
                
                .frame(height:280)
            
      

这是我在另一个视图中的文本或圣经()

import SwiftUI

struct Scripture: View 

    //@EnvironmentObject var fsc: FontSizeChanging
    
    var body: some View 
        ScrollView 
        
            Text("Hih in Zeisu Khazih khang ciamtehna ahi hi. Amah in David tapa hi a, David in Abraham tapa ahi hi. 2 Abraham tapa Isaac, Isaac tapa Jacob, Jacob tapa Judah le a sanggam pasal teng ahi hi. 3 Judah in a zi Tamar tawh a neih a tapa Perez le Zerah, Perez tapa Hezron, Hezron tapa Ram, 4 Ram tapa Amminadab, Amminadab tapa Nahshon, Nahshon tapa Salmon, 5 Salmon in a zi Rahab tawh a neih a tapa Boaz, Boaz in a zi Ruth tawh a neih a tapa Obed, Obed tapa Jesse, 6 Jesse tapa kumpipa David ahi hi. David in Uriah zi tawh a neih a tapa Solomon, 7 Solomon tapa Rehoboam, Rehoboam tapa Abijah, Abijah tapa Asa, 8 Asa tapa Jehoshaphat, Jehoshaphat tapa Joram, Joram tapa Uzziah, 9 Uzziah tapa Jotham, Jotham tapa Ahaz, Ahaz tapa Hezekiah, 10 Hezekiah tapa Manasseh, Manasseh tapa Amon, Amon tapa Josiah ahi hi. 11 Jeconiah le a sanggam pasal tengin Babylon gamah sala a kipaipih madeuh in a suak Josiah tate ahi uh hi. 12 Babylon gama saltan khit ciangin Jeconiah in a neih a tapa Shealtiel, Shealtiel tapa Zerubbabel, 13 Zerubbabel tapa Abiud, Abiud tapa Eliakim, Eliakim tapa Azor, 14 Azor tapa Zadok, Zadok tapa Achim, Achim tapa Eliud, 15 Eliud tapa Eleazar, Eleazar tapa Matthan, Matthan tapa Jacob, 16 Jacob tapa Joseph hi a, Joseph in Khazih a kici Zeisu a hong suahna Mary pasal ahi hi. 17 Tua hi a, Abraham khang panin David khang ciang a vekin khang sawm le khang li, David khang panin Babylon gama saltang dinga a kimat hun ciang khang sawm le khang li, Babylon gama saltan hun pan Khazih khang ciang khang sawm le khang li mah ahi hi. 18 Zeisu Khazih a suahzia hih bang ahi hi. A nu Mary pen Joseph zi dingin zuthawl kipiasa hi a, a kiteenma un Kha Siangtho hangin gai hi ci in kithei hi. 19 A lawmpa Joseph in mi lungsim hoih hi a, a lawmnu Mary min daisak nuamlo ahih manin maksim dingin a ngaihsun hi.")
                //.font(fsc.fontSize)
                .foregroundColor(.primary)
                //.font(Setting(fontSizeIndex: self.$fontSizeIndex))
                .multilineTextAlignment(.leading) // test .center here
                .lineSpacing(5)
                .padding()
                
        
    


struct Scripture_Previews: PreviewProvider 
    static var previews: some View 
        Scripture()
    

【问题讨论】:

【参考方案1】:

以下应该可以工作。

我在屏幕右上角添加了一个齿轮图标,以使设置和圣经之间的导航更容易。这将允许您轻松更改字体大小。

我的方法使用@AppStorage。这会保存数据,因此当用户启动应用程序时,他们的字体大小选择将保留。您可以在任何需要编辑字体大小的地方使用它。

要更改字体大小,只需更新选择器内.tag() 中的值即可。我随机选择了一组数字。这是您要输入所需字体大小的地方。

 import SwiftUI

struct Setting: View 
    @State private var fontSizeIndex = 3
    @State private var fontIndex = 1
    @AppStorage("fontSize") var fontSize = 50
    
    
    var body: some View 
        NavigationView 
            ScrollView 
                
                // Text Settings
                Form 
                    
                    // Font Size Picker
                    Section(header: Text("Text Settings")) 
                        
                        Picker(selection: $fontSize, label: Text("Font Size"),
                               content: 
                            Text("Extra Small").tag(10)
                            Text("Small").tag(15)
                            Text("Medium").tag(20)
                            Text("Large").tag(25)
                            Text("Extra Large").tag(50)
                        )
                        
                        
                        // Font Picker
                        // I plan to make custom fonts if I know how to update my
                        // selection
                        Picker(selection: $fontIndex, label: Text("Font"), content: 
                            Text("Font1").tag(1)
                            Text("Font2").tag(2)
                            Text("Font3").tag(3)
                            Text("Font4").tag(4)
                            Text("Font5").tag(5)
                            Text("Font6").tag(6)
                            Text("Font7").tag(7)
                        )
                        
                    
                
                .frame(height:280)
            
        
    


struct Scripture: View 
    
    //@EnvironmentObject var fsc: FontSizeChanging
    
    
    @AppStorage("fontSize") var fontSize = 50
    
    var body: some View 
        NavigationView 
            
            ScrollView 
                
                Text("Hih in Zeisu Khazih khang ciamtehna ahi hi. Amah in David tapa hi a, David in Abraham tapa ahi hi. 2 Abraham tapa Isaac, Isaac tapa Jacob, Jacob tapa Judah le a sanggam pasal teng ahi hi. 3 Judah in a zi Tamar tawh a neih a tapa Perez le Zerah, Perez tapa Hezron, Hezron tapa Ram, 4 Ram tapa Amminadab, Amminadab tapa Nahshon, Nahshon tapa Salmon, 5 Salmon in a zi Rahab tawh a neih a tapa Boaz, Boaz in a zi Ruth tawh a neih a tapa Obed, Obed tapa Jesse, 6 Jesse tapa kumpipa David ahi hi. David in Uriah zi tawh a neih a tapa Solomon, 7 Solomon tapa Rehoboam, Rehoboam tapa Abijah, Abijah tapa Asa, 8 Asa tapa Jehoshaphat, Jehoshaphat tapa Joram, Joram tapa Uzziah, 9 Uzziah tapa Jotham, Jotham tapa Ahaz, Ahaz tapa Hezekiah, 10 Hezekiah tapa Manasseh, Manasseh tapa Amon, Amon tapa Josiah ahi hi. 11 Jeconiah le a sanggam pasal tengin Babylon gamah sala a kipaipih madeuh in a suak Josiah tate ahi uh hi. 12 Babylon gama saltan khit ciangin Jeconiah in a neih a tapa Shealtiel, Shealtiel tapa Zerubbabel, 13 Zerubbabel tapa Abiud, Abiud tapa Eliakim, Eliakim tapa Azor, 14 Azor tapa Zadok, Zadok tapa Achim, Achim tapa Eliud, 15 Eliud tapa Eleazar, Eleazar tapa Matthan, Matthan tapa Jacob, 16 Jacob tapa Joseph hi a, Joseph in Khazih a kici Zeisu a hong suahna Mary pasal ahi hi. 17 Tua hi a, Abraham khang panin David khang ciang a vekin khang sawm le khang li, David khang panin Babylon gama saltang dinga a kimat hun ciang khang sawm le khang li, Babylon gama saltan hun pan Khazih khang ciang khang sawm le khang li mah ahi hi. 18 Zeisu Khazih a suahzia hih bang ahi hi. A nu Mary pen Joseph zi dingin zuthawl kipiasa hi a, a kiteenma un Kha Siangtho hangin gai hi ci in kithei hi. 19 A lawmpa Joseph in mi lungsim hoih hi a, a lawmnu Mary min daisak nuamlo ahih manin maksim dingin a ngaihsun hi.")
                //.font(fsc.fontSize)
                    .font(.system(size: CGFloat(fontSize)))
                    .foregroundColor(.primary)
                //.font(Setting(fontSizeIndex: self.$fontSizeIndex))
                    .multilineTextAlignment(.leading) // test .center here
                    .lineSpacing(5)
                    .padding()
                
            
            .toolbar 
                ToolbarItem(placement: .navigationBarTrailing) 
                    HStack 
                        NavigationLink(destination: Setting(), label: 
                            Image(systemName: "gearshape")
                        )
                    
                    
                
            
            
        
    


struct Scripture_Previews: PreviewProvider 
    static var previews: some View 
        Scripture()
    

【讨论】:

您只需在一分钟内解决 4 天的头痛问题。非常感谢。 如果解决方案有效,请将其标记为已接受的答案。谢谢! 等等.. 我是 Stack Overflow 的新手。如何标记已接受的答案? @Lian 要执行此操作,您可以单击答案旁边的复选标记。选择后将变为绿色。你可以在这里阅读更多:***.com/help/someone-answers

以上是关于如何在swiftui中使用选择器更改文本大小的主要内容,如果未能解决你的问题,请参考以下文章

如何更改GraphicalDatePickerStyle日期选择器Swiftui的选择颜色(AM,PM)和时间

如何在日期时间选择器对话框中更改天数的文本大小?

如何更改日期选择器中的文本大小?

如何更改 UIPickerView 选择器的颜色

如何在 SwiftUI 中将颜色选择器值保存到 UserDefaults 中?

从选择器 SwiftUI 中读取选择