|
How do I modify a view's attribute based on certain output values? For example: I have a Text(deriveVibration(string: profile.name)) that returns a calculated value(string) based on textfield input. If the result is 4, 6 or 8, I want the Text to have the attribute of .foregroundColor(Color.purple) If the result is 0, the same Text will then change the attribute to .foregroundColor(Color.white) Else, return Text with an attribute of .foregroundColor(Color.red) I appreciate anyone's input on this. Thank you! |
|
Hi Sally, So this might change the way you have your project set up but to see what you think copy and paste somewhere and run it in your preview -
So if you type 0 the text will be red, if 1 then text is green and anything else will be blue. This is a very very basic example and hopefully what you are after. Now there are many ways you could implement this. The class is where you would probably put your derive functions as well. Then you could just call myText.derive for the textfields etc Play around with it and see how you go. Dave |
|
Thanks for the reply. I tried running this after pasting it but didn't seem to work. The Text I need to modify is the text value after a function has been used to return a new value. I can access the profile.name which is the observable object. But the one I need to modify is the Text that has been recalculated to return a new value. Is there a way to get the value binding from this Text(deriveVibration(string: self.profile.name))? |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
you could try adding another @Published property to your observable object called color
then create a function for the observableobject called setColor
Then in your derive function, call the above function passing to it the result you calculated in the derive function, so probably just before your return statement of the derive function. So all together you could distegard what i posted above and try. Once above is done you can now put this in your foreground modifier
Okay.... what should happen.
Hopefully that all made sense Sally. Give it a go and let me know if it suits Dave |
|
Hi Sally, The modifier ( Without seeing the code that you have make it difficult to see the flow. |
|
I appreciate you both for your suggestions. Nigelgee, How do I create a State var for the new calculated value? I tried implementing what you suggested Dave but I'm a bit confused as to how to call this function in my derive function. To give you both some clarification. I'll post the necessary code below...
When I run this, it fails to build... maybe I wasn't allowed to have the functions in the body |
|
Hi Sally, My last example was if you had your derive functions and setColor functions inside your Profile Observable Ojbect. Since you have put them in the Content View struct you will have to change the following. The reason why it doesn;t work at the moment is because of this line -
You are declaring a constant SetColor (just on a side note be careful of your naming conventions for variables, constants etc - For classes, structs etc it starts with capital let but for vars and lets it starts with lower case. In the above line it should of been setColor not SetColor. But because your func is called that it can get confusing so probably stick with just 'color') So back to that line of code. All you are doing here is creating a constant which is a Color which is returned by calling that function. Because you haven;t assigned it to any view property then no changes will be refelcted etc. So based on what you have showed me lets delete that line. Lets also delete the color property in your Profile object. So now you will just have your deriveVibration and setColor functions in your content view. Make sure they are placed outside the var body property. So the functions should be placed after this. Now with your setColor func and the switch statement. If you have a number of cases in a switch statement that return the same Color you can combine them, so change the func to the following -
You can keep your deriveVibration func the same as it was before (remember we now just delete that let SetColor statement). -
Now you can leave your Text views how you had it before -
And now all we do is call the setColor func, passing in the result of the deriveVibration func like so -
So like the text view when the user types in the TextField, the profile.name changes. By calling setColor passing the return result of calling deriveVibration on profile.name, this will ensure that every time user types into TextField the color will change. Now one important thing to remember, the order of modifiers really do matter. This is a subject on its own so i dont wont to get into it too much here. It is not an issue here with your code because .foregroundColor is your last modifier on that text view, but if you had a number of other modifiers mixed in with it then you may not see the desired results. As I said shouldn;t be an issue with your code as it is. So after all that make the changes and see how you go Dave |
|
Hi Sally Dave solution looks good and should work. My only corcern is that you are calling the func twice when you could return the string. Thanks Dave for pointing that what I said was correct but think you misinterpreted when I said a computed property was a @State this is what I meant
PS you know that you are missing "2" |
|
Hey Nigel, yeah my bad i did misinterpret :( Sorry about that. I realised and deleted that reply. Sally, Nigels solution also looks good and maybe better. This is whats good about talking with others and throwing around solutions. Youll find heaps of different ways to tackle a problem and at the end of the day we all learn something. |
|
Dave, I'm so happy it works! Thanks again! I just forgot to mention the value i switched is just the last character of the string but I added it to the function now. Thank you so much! |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.