Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SwiftUITextOutputFormat to output a Text struct #92

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

andreweades
Copy link

This OutputFormat outputs a Text struct that can be used in a SwiftUI view.
It ignores the font as you normally would apply the font with a .font modifier in SwiftUI.
Either we should convert the theme's font to a SwiftUI.Font and and use that or leave it to the user to style fonts separately from highlighting syntax colours. I favour the later as it fits better with the model of SwiftUI and leaves SwiftUITextOutputFormat to be only concerned with constructing the highlighted text but this might be confusing as it ignores the theme's font. However, the attributed text string is different to the font and they could be considered separate and treated separately as SwiftUi pushes us in that direction and an additional .font modifier would override any theme font anyway. Maybe this should really be a view modifier?

@andreweades
Copy link
Author

andreweades commented Jan 3, 2020

Apologies. I had some CI build issues as I was rushing like an idiot and just uploaded the file thinking it would work. It obviously builds on my Catalina set up for SwiftUI development. I'm getting a 404 so I can't tell what the other fail is.

@JohnSundell
Copy link
Owner

Hey @andreweades, thanks for contributing to Splash. I'll try to review this as soon as possible. Just to help you with the CI issues before then, rather than wrapping the entire implementation in conditional compilation, I suggest using the @available attribute on the new format. That should give you a cleaner implementation as well. If you want to verify your changes locally, just build Splash for the iOS simulator for an iOS version lower than 12, and you should see the same errors as on macOS Mojave.

@andreweades
Copy link
Author

andreweades commented Jan 3, 2020 via email

@JohnSundell
Copy link
Owner

Would love for @duemunk to take a look at this, since I know that he's been working on something similar 🙂 Will also need this to be covered with tests before it can be merged in, just a heads-up. But I'll try to review this within the next few days.

@duemunk
Copy link
Contributor

duemunk commented Jan 3, 2020

Looks really great!! Much simpler than my implementation. Particularly this is super clever:

texts.reduce(Text(""), +)

Great when SwiftUI can surprise in positive ways like that.

Copy link
Contributor

@duemunk duemunk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation doesn't support the theme font.

@andreweades
Copy link
Author

andreweades commented Jan 3, 2020

The implementation doesn't support the theme font.

That's right. I had some questions regarding font support as the SwiftUI way would be to use the .font view modifier. We could internalise this in the SwiftUITextOutputFormat or leave it to the user. I noted in my PR that this would be confusing so needs more thought.

Options:

  1. internalise font support and maintain the API
  2. separate colour theme from font selection as SwiftUI would encourage

But then I thought maybe it should just be a view modifier that can be applied to any Text and you would get something like the following:

Text(stringOfCode).syntaxHighlight(.presentation).font(.system(size: 12, design: .monospaced))

which leaves the font selection to the existing modifier.

@andreweades
Copy link
Author

After thinking about this a bit more... Maybe do 1 and make a syntaxHighlight view modifier that highlights and applies a font modifier.

@andreweades
Copy link
Author

My use case is demoed here.

I highlight the main text and the minimap using the same theme but with a different font applied to each.

@duemunk
Copy link
Contributor

duemunk commented Jan 12, 2020

I think using the theme's font is a good approach. The extensions are a nice to have in my opinion.

@andreweades
Copy link
Author

I will look into this again when I have some spare time. I will have to schedule some Open Source hours.

@Jeehut
Copy link

Jeehut commented May 5, 2022

@andreweades Any plans to get this to completion?

@andreweades
Copy link
Author

@andreweades Any plans to get this to completion?

I think SwiftUI support for AttributedString supplants this.

@Jeehut
Copy link

Jeehut commented May 5, 2022

@andreweades
But how to change the design of the font? I'd like to use .system(design: .monospaced), but this didn't work:

Text(
  AttributedString(
    SyntaxHighlighter(format: AttributedStringOutputFormat(theme: .sundellsColors(withFont: .init(size: 13))))
      .highlight(viewStore.codePreview)
    )
)
.font(.system(size: 13, design: .monospaced)) // <-- `.monospaced` gets ignored

Or is there a simple way to provide a SwiftUI.Font to the withFont which expects a Splash.Font?

@andreweades
Copy link
Author

andreweades commented May 5, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants