SwiftUI and Attributed Text

One key of attributedText is this


this let you to write text in vertically way. example, Japanese or Chinese text.

this key unfortunately is only for UIKit.
So, Is there a way to print some text in swiftUI obtaining the same result?

Thank you


Even though it’s ‘not available’ for iOS, it still works. The following worked for me:

import SwiftUI

struct VerticalText: UIViewRepresentable {
var string: String
var highlight: String
var fontSize: CGFloat

func makeUIView(context: Context) -> UILabel {
    let v = UILabel()
    return v

func updateUIView(_ uiView: UILabel, context: Context) {
    let selected: [NSAttributedString.Key : Any] =
        [ NSAttributedString.Key.verticalGlyphForm: true,
          NSAttributedString.Key.foregroundColor: UIColor(.secondary) ]

    let notSelected: [NSAttributedString.Key : Any] =
        [ NSAttributedString.Key.verticalGlyphForm: true,
          NSAttributedString.Key.foregroundColor: UIColor(.secondary) ]
    let highlighted: [NSAttributedString.Key : Any] =
        [ NSAttributedString.Key.verticalGlyphForm: true,
          NSAttributedString.Key.foregroundColor: UIColor(Color("AccentColor")) ]
    var mas: NSMutableAttributedString
    if let range = string.range(of: highlight, options: [.diacriticInsensitive]) {
        let a = string[..<range.lowerBound]
        let b = string[(range)]
        let c = string[range.upperBound...]
        let ma = NSMutableAttributedString(
            string: String(a),
            attributes: selected
        let mb = NSMutableAttributedString(
            string: String(b),
            attributes: highlighted
        let mc = NSMutableAttributedString(
            string: String(c),
            attributes: selected
        mas = ma
    } else {
        mas = NSMutableAttributedString(
            string: string,
            attributes: notSelected
    uiView.attributedText = mas
    uiView.numberOfLines = 0
    uiView.preferredMaxLayoutWidth = fontSize
    uiView.font = UIFont(name: "UDDigiKyokashoNP-B", size: fontSize)
    uiView.setContentHuggingPriority(.defaultHigh, for: .vertical)
    uiView.setContentHuggingPriority(.defaultHigh, for: .horizontal)


This topic was automatically closed after 166 days. New replies are no longer allowed.