mos Posted July 13, 2020 Posted July 13, 2020 I have a MainForm which contains a TUniLabel called lblHeader. In my external CSS file I have the following: .lblHeader { color: red !important; } Now if I don't have the !important the red color does not get applied to the label. The issue I have is I need to in code to be able to change the color of the label from red to blue in code. I tried the following code but it did not work: lblHeader.JSInterface.JSCode(#1'.setStyle("color","blue");'); The above does work if I remove the !important for the red but the color red doesn't get applied. I also tried: lblHeader.JSInterface.JSCode(#1'.setStyle("color","blue !important");'); But this does not work. How can I get the label to change from red to blue at runtime? Quote
Sherzod Posted July 13, 2020 Posted July 13, 2020 4 hours ago, mos said: How can I get the label to change from red to blue at runtime? Hello, You can try to use without customCSS: 1. UniFormCreate: procedure TMainForm.UniFormCreate(Sender: TObject); begin UniLabel1.JSInterface.JSConfig('style', ['color: red']); end; 2. Runtime: procedure TMainForm.UniButton1Click(Sender: TObject); begin UniLabel1.JSInterface.JSCall('setStyle', ['color', 'blue']); end; Quote
mos Posted July 13, 2020 Author Posted July 13, 2020 Hi Sherzod, The reason I need to have the color set in the external CSS file is so that I don't need to recompile the exe for different sites and can just change the CSS as needed. I also have a configuration dialog where the user can set the colors that will override the external CSS file colors for some labels etc. if they choose not to modify the CSS. Now the code using setStyle does work if the !important is removed but removing that causes the red to not be applied to the label. Quote
Sherzod Posted July 13, 2020 Posted July 13, 2020 2 minutes ago, mos said: Now the code using setStyle does work if the !important is removed but removing that causes the red to not be applied to the label. Then please make a simple testcase to check. Quote
mos Posted July 13, 2020 Author Posted July 13, 2020 Hi Sherzod, Attached is a test project with an external CSS file. If you run this project the label should change to red but it doesn't. If you modify the css and add !important it will change it to red. CSSTest.zip Quote
Sherzod Posted July 14, 2020 Posted July 14, 2020 1 hour ago, mos said: If you run this project the label should change to red but it doesn't. If you modify the css and add !important it will change it to red. Hi, I will check. Quote
Sherzod Posted July 14, 2020 Posted July 14, 2020 4 hours ago, mos said: Attached is a test project with an external CSS file. Are you sure that your css file is in the right path? Quote
mos Posted July 14, 2020 Author Posted July 14, 2020 Hi Sherzod, Yes it is. I can only get the label to change color to red if I have !important after it in the CSS file. Quote
Sherzod Posted July 14, 2020 Posted July 14, 2020 17 minutes ago, mos said: I can only get the label to change color to red if I have !important after it in the CSS file. Yes. OK, try this approach: function afterCreate(sender) { sender.setStyle('color', ''); } Quote
mos Posted July 14, 2020 Author Posted July 14, 2020 Hi Sherzod, That worked. Thanks again for your help. 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.