nicklockwood / layout

A declarative UI framework for iOS
MIT License
2.23k stars 97 forks source link

Setting any UIView property at runtime from string property name and string property value #185

Open cadrega opened 4 years ago

cadrega commented 4 years ago

Hi @nicklockwood ! Layout is really great at creating completely custom and dynamically server-side created GUIs.

However, I would like to alter the GUI appearance at runtime, based on a server-side implementation. To do so, in my ViewController which hosts a mounted LayoutNode, I created a method that will be called after receiving a message from the network:

func setViewValue(tag: Int, valueName: String, newValue: String) {
    guard let baseView = self.view else {
        return
    }

    if let v = baseView.viewWithTag(tag) {
        try? v.setValue(newValue, forExpression:valueName)
    }
}

This already works flawlessly for String properties, like "title". However, I would like to set any property type just as Layout does in XML. I don't need to handle complicated expressions, just be able to specify a UIColor, false/true, geometry or number values...

It seems to me that the steps would be:

However, many of these methods or even entire structs (LayoutExpression) are declared internal. Is there a ready-made method for doing this? Am I missing something? Or a major rework would be needed to do this? If yes, can you point me in the right direction? I can come back with a pull request once I have this finished. Thanks!

cadrega commented 4 years ago

This was easier than I thought. I'm a swift newbie, so my code could be surely improved, but here's the pull request about the new method on UIView+Layout: #186