Monday, February 12, 2024
HomeiOS Developmentios - Learn how to apply negative effects in a swiftui ViewBuilder

ios – Learn how to apply negative effects in a swiftui ViewBuilder


I’m making an attempt to render a easy Textual content() and apply an arbitrary variety of totally different kinds .
That is my code:

struct Cell {
    var content material: String
    var model: TextStyle
}

@ViewBuilder
func styledText(cell: Cell) -> some View {
    if (cell.model.incorporates(TextStyle.rtl)) {
        Textual content(cell.content material)
            .body(maxWidth: .infinity, alignment: .main)
            .atmosphere(.layoutDirection, .rightToLeft)
            .lineSpacing(10)
    } else if (cell.model.incorporates(TextStyle.customFont)) {
        Textual content(cell.content material).font(.customized("MyFont", measurement: 19))
    } else if (cell.model.incorporates(TextStyle.pinkColour)) {
        Textual content(cell.content material).foregroundStyle(Coloration(UIColor.systemPink))
    } else {
        Textual content(cell.content material)
    }
}

You may see from the above that just one department can execute within the above operate. However in my app, a number of kinds may very well be utilized (e.g. BOTH customFont and pinkColour). How do I obtain this?

What I attempted

@ViewBuilder
func styledText(cell: Cell) -> some View {
    var textual content = Textual content(cell.content material)
    if (cell.model.incorporates(TextStyle.rtl)) {
        textual content = textual content
            .body(maxWidth: .infinity, alignment: .main)
            .atmosphere(.layoutDirection, .rightToLeft)
            .lineSpacing(10)
    } else if (cell.model.incorporates(TextStyle.customFont)) {
        textual content = textual content.font(.customized("MyFont", measurement: 19))
    } else if (cell.model.incorporates(TextStyle.pinkColour)) {
        textual content = textual content.foregroundStyle(Coloration(UIColor.systemPink))
    }
    textual content
}

So far as I perceive (I am new to Swift), each department should lead to a return worth so the above doesn’t work.

I additionally tried not utilizing @ViewBuilder in any respect, however I can not get my code to kind examine. I get errors resembling Can not assign worth of kind 'some View' (results of 'Self.font') to kind 'some View' (kind of 'textual content')

What am I lacking?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments