swiftui

How to know which line of a TextView the user has tapped on

杀马特。学长 韩版系。学妹 提交于 2021-01-28 07:09:31
问题 I'm building an app that has a multi-line text editor (UITextView in UIKit, and TextEditor in SwiftUI). When the user tap on any line of the multi-line text, I should know which line is that, so I can print that line or do any other task. Is there a way to know which line of a UITextView the user has tapped on (using textViewDidChangeSelection delegate function or anything else)? I was thinking of storing the whole text in an Array (each line is an element), and updating the array

How do I change the fixed size of a Lazy Grid GridItem in response to Dynamic Text size changes?

老子叫甜甜 提交于 2021-01-28 06:14:05
问题 The code below is using a LazyVGrid to implement layout of my controls such that everything lines up like this: Particularly, the sliders' ends all align and the symbols are centre-aligned to each other. I have worked out how to size the GridItems for the symbol and the numeric readout such that they are the 'right size' for their contents — something neither flexible nor adaptive GridItems will do — while accounting for Dynamic Type . In testing, this works really well so long as the user

can I get the tap position over a Text View?

只愿长相守 提交于 2021-01-28 05:30:33
问题 a Text view is like: Text("hello world") .onTapGesture{ ...} can I get the tab position ,so that to infer which word is tapped. 回答1: Here is an approach how to get tap location in Text view coordinate space: Text("hello world") .gesture(DragGesture(minimumDistance: 0).onEnded { value in print("Position: \(value.location)") // in local coordinates }) 来源: https://stackoverflow.com/questions/62152160/can-i-get-the-tap-position-over-a-text-view

Swift - Get array item with an IndexSet

点点圈 提交于 2021-01-28 05:26:21
问题 I have my Contact object: struct Contact: Codable, Identifiable { var id: Int = 0 var name: String var lastName: String var phoneNumber: String } And in my view I have a list of contacts that will be fetched from a server. List { ForEach(viewModel.contacts) { contact in ContactView(contact: contact) } .onDelete(perform: self.viewModel.delete) } When I delete a contact I call my viewModel method delete wich only removes the item from the array. But since I will make a server request to delete

SwiftUI Font smallCaps() not working with italic()

时光总嘲笑我的痴心妄想 提交于 2021-01-28 05:22:26
问题 extension Font { public static let Heading1: Font = system(size: 34, weight: .black, design: .default) .smallCaps() .italic() } I can make the font all caps OR italic - but not both. Anybody know how to get around this? Without typing the String out in caps to begin with. I even tried importing BlackItalic as a custom font and still had the same issue. 回答1: None of Apple platform built-in font support both italic-smallcap features combination. System San Francisco is the most font-feature

Is it possible to hide backfaces when using SwiftUI rotation3DEffect?

我与影子孤独终老i 提交于 2021-01-28 05:18:54
问题 Consider this simple example: import SwiftUI struct RotationExample: View { var body: some View { ZStack { Color.red Text("Text I want to hide") .font(.system(size: 48)) } .frame(width: size, height: size) .rotation3DEffect(Angle.degrees(180), axis: (x: 0, y:1, z: 0)) } } struct RotationExample_Previews: PreviewProvider { static var previews: some View { RotationExample() } } When this code is run, the text is shown reversed. I thought when the rotation was applied, the layers would be

Multiple enum types list all cases

…衆ロ難τιáo~ 提交于 2021-01-28 05:13:47
问题 can't wrap my head around this and can't find any answers, hope someone could point me in the right direction. I have multiple enums. Some examples(i have a lot more and also more complex enums): enum Fish: String, CaseIterable, Displayable { case goldfish case blueTang = "blue_tang" case shark } enum Tree: String, CaseIterable, Displayable { case coconut case pine case englishOak = "english_oak" } I want to display those in a list with sections. I'm using swiftUI but that probably doesn't

How to share published model between two view models in SwiftUI?

谁说胖子不能爱 提交于 2021-01-28 05:05:25
问题 I am trying to access the same shared model within two different view models. Both associated views need to access the model within the view model and need to edit the model. So I can't just use the EnvironmentObject to access the model. I could pass the model to the view model through the view, but this wouldn't keep both model versions in sync. Is there something that could work like binding? Because with binding I can access the model but then it won't publish the changes in this view.

Can @AppStorage be used in the Environment in SwiftUI?

老子叫甜甜 提交于 2021-01-28 05:05:14
问题 Can @AppStorage be used in the Environment in SwiftUI, if so, how would you do it? I know you can send the value for the @AppStorage from one view to another using @Bindings as a general wondering I would like to know if its possible to put it in the environment. I don't have a practical example as to when this would be applicable, but I was wondering if it was possible. Would this be crazy enough to work? I think you will only store the value and it won't be stored in the UserDefault. struct

How to share published model between two view models in SwiftUI?

好久不见. 提交于 2021-01-28 05:04:54
问题 I am trying to access the same shared model within two different view models. Both associated views need to access the model within the view model and need to edit the model. So I can't just use the EnvironmentObject to access the model. I could pass the model to the view model through the view, but this wouldn't keep both model versions in sync. Is there something that could work like binding? Because with binding I can access the model but then it won't publish the changes in this view.