Home > OS >  How to change font size of UTF16(Gujarati language) attributed string in function and use it to conv
How to change font size of UTF16(Gujarati language) attributed string in function and use it to conv

Time:01-27

i'm currently showing json data in SwiftUI list using function:

@available(iOS 15, *)
func attributedString(from str: String) -> AttributedString {
    
    if let theData = str.data(using: .utf16 ) {
        do {
          
          
            let theString = try NSAttributedString( data: theData, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil )
            
   
            return AttributedString(theString)
        } catch {  print("\(error)")  }
    }
    return AttributedString(str)
}

and putting it in

import SwiftUI

struct lvl4: View {
    @State var books: [Buk] = [] 
    @State var selection: Buk?
  

 

    
    //ios 14 must to get the syntex right..
    @available(iOS 14, *)
    var body: some View {
        
        
        NavigationView {

            List(books) { book in
                ForEach(book.bookContent) { bookContent in
                    Section(header: Text(bookContent.title).font(.largeTitle) .fontWeight(.heavy)) {
                        OutlineGroup(bookContent.child, children: \.child) { item in
                            if #available(iOS 15, *) {
                               
                                Text(attributedString(from: item.title))

                        }
                    }
                }
            }
            
  

    
    // a file with valid json data for testing
    func loadData() {
        do {
            if let url = Bundle.main.url(forResource: "સાગર મંથન_new", withExtension: "json") {
                let data = try Data(contentsOf: url)
                books = try JSONDecoder().decode([Buk].self, from: data) 
            }
        } catch {
            print("error: \(error)")
        }
    }
    
}

struct Buk: Identifiable, Codable {
    let id = UUID()
    var bookTitle: String = ""
    var isLive: Bool = false
    var userCanCopy: Bool = false
    var bookContent: [BookContent] = []
    
    enum CodingKeys: String, CodingKey {
        case bookTitle = "book_title"
        case isLive = "is_live"
        case userCanCopy = "user_can_copy"
        case bookContent = "book_content"
    }
}

struct BookContent: Identifiable, Codable {
    let id = UUID()
    var title, type: String
    var child: [Child]
}

struct Child: Identifiable, Codable {
    let id = UUID()
    var title, type: String
    var child: [Child]?
}

i need to customise the font and size of the attributed string before it is returned as below which are very small by default when it is converted from utf16.. The code is for swiftUI and the function i created is for using it in parsing json file

enter image description here very small text..

CodePudding user response:

I thought you would make an effort and find the answer yourself, but I can see you could not. Try this:

func attributedString(from str: String, font: Font) -> AttributedString {
    if let theData = str.data(using: .utf16) {
        do {
            let theString = try NSAttributedString(data: theData, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
            var attaString = AttributedString(theString)
            attaString.font = font  // <-- here
            return attaString
        } catch {
            print("\(error)")
        }
    }
    return AttributedString(str)
}

and call it like this:

Text(attributedString(from: item.title, font: Font.system(size: 30)))
  •  Tags:  
  • Related