How do I make an attributed string using Swift?

前端 未结 28 2284
耶瑟儿~
耶瑟儿~ 2020-11-22 10:11

I am trying to make a simple Coffee Calculator. I need to display the amount of coffee in grams. The \"g\" symbol for grams needs to be attached to my UILabel that I am usin

28条回答
  •  庸人自扰
    2020-11-22 10:50

    It will be really easy to solve your problem with the library I created. It is called Atributika.

    let calculatedCoffee: Int = 768
    let g = Style("g").font(.boldSystemFont(ofSize: 12)).foregroundColor(.red)
    let all = Style.font(.systemFont(ofSize: 12))
    
    let str = "\(calculatedCoffee)g".style(tags: g)
        .styleAll(all)
        .attributedString
    
    label.attributedText = str
    

    You can find it here https://github.com/psharanda/Atributika

提交回复
热议问题