@@ -107,10 +107,8 @@ struct CodeBlock: View {
107107 ForEach ( 0 ..< viewModel. suggestion. count, id: \. self) { index in
108108 Text ( " \( index + viewModel. startLineIndex + 1 ) " )
109109 . foregroundColor ( Color . white. opacity ( 0.6 ) )
110- Text ( AttributedString ( viewModel. suggestion [ index] ) )
110+ BreakByCharText ( viewModel. suggestion [ index] )
111111 . frame ( maxWidth: . infinity, alignment: . leading)
112- . multilineTextAlignment ( . leading)
113- . lineSpacing ( 4 )
114112 }
115113 }
116114 . foregroundColor ( . white)
@@ -234,3 +232,38 @@ struct SuggestionPanelView_Preview: PreviewProvider {
234232 }
235233 }
236234}
235+
236+ // Super dirty hack to workaround that AttributedText doesn't support paragraphStyle.lineBreakMode
237+ struct BreakByCharText : View {
238+ let attributedString : NSAttributedString
239+ let attributedChars : [ NSAttributedString ]
240+
241+ init ( _ attributedString: NSAttributedString ) {
242+ self . attributedString = attributedString
243+ attributedChars = splitAttributedString ( attributedString)
244+ }
245+
246+ var body : some View {
247+ LazyVGrid (
248+ columns: [ . init( . adaptive( minimum: 8 ) , spacing: 0 , alignment: . leading) ] ,
249+ alignment: . leading,
250+ spacing: 4
251+ ) {
252+ ForEach ( 0 ..< attributedChars. endIndex, id: \. self) { index in
253+ Text ( AttributedString ( attributedChars [ index] ) )
254+ }
255+ }
256+ }
257+ }
258+
259+ private func splitAttributedString( _ inputString: NSAttributedString ) -> [ NSAttributedString ] {
260+ var output = [ NSAttributedString] ( )
261+ var start = 0
262+ for char in inputString. string {
263+ let range = NSMakeRange ( start, char. utf16. count)
264+ let attributedString = inputString. attributedSubstring ( from: range)
265+ output. append ( attributedString)
266+ start += range. length
267+ }
268+ return output
269+ }
0 commit comments