I am trying to render a simple Text() and apply an arbitrary number of different styles .
This is my code:
struct Cell {
var content: String
var style: TextStyle
}
@ViewBuilder
func styledText(cell: Cell) -> some View {
if (cell.style.contains(TextStyle.rtl)) {
Text(cell.content)
.frame(maxWidth: .infinity, alignment: .leading)
.environment(\.layoutDirection, .rightToLeft)
.lineSpacing(10)
} else if (cell.style.contains(TextStyle.customFont)) {
Text(cell.content).font(.custom("MyFont", size: 19))
} else if (cell.style.contains(TextStyle.pinkColour)) {
Text(cell.content).foregroundStyle(Color(UIColor.systemPink))
} else {
Text(cell.content)
}
}
You can see from the above that only one branch can execute in the above function. But in my app, several styles could be applied (e.g. BOTH customFont and pinkColour). How do I achieve this?
What I tried
@ViewBuilder
func styledText(cell: Cell) -> some View {
var text = Text(cell.content)
if (cell.style.contains(TextStyle.rtl)) {
text = text
.frame(maxWidth: .infinity, alignment: .leading)
.environment(\.layoutDirection, .rightToLeft)
.lineSpacing(10)
} else if (cell.style.contains(TextStyle.customFont)) {
text = text.font(.custom("MyFont", size: 19))
} else if (cell.style.contains(TextStyle.pinkColour)) {
text = text.foregroundStyle(Color(UIColor.systemPink))
}
text
}
As far as I understand (I'm new to Swift), every branch must result in a return value so the above does not work.
I also tried not using @ViewBuilder at all, but I cannot get my code to type check. I get errors such as Cannot assign value of type 'some View' (result of 'Self.font') to type 'some View' (type of 'text')
What am I missing?
You can extract a view modifier for each style, which will take a
Booland decide whether to return a modified view, orself.Alternatively, always apply all the modifiers, and use some default values when particular
TextStyles are not instyle.