SwiftUI AttributedString on Watch


Gerriet M. Denkmann
 

I have a Watch-App using SwiftUI.

let fontSize = 40.0
struct WatchContentView: View
{
@State var timeAttributedStr = AttributedString("--")
let timer = Timer.publish(every: 1, tolerance: 0.1, on: .main, in: .common).autoconnect()

var body: some View
{
VStack
{
Text(timeAttributedStr)
}
.onReceive(timer)
{ currentTime in

timeAttributedStr = … make from currentTime using fontSize …

// compare size of timeAttributedStr to window and decrease fontSize if needed
// no idea how to do this
}
}
}

The Problem:
I want to make fontSize as big as possible. And then do:
if timeAttributedStr.size.width > window.size.width then decrease fontSize

But sadly, AttributedString has no size.

A fixed fontSize is less than ideal, because the optimal timeAttributedStr will depend both on Locale and Watch model.

Gerriet.

Join cocoa@apple-dev.groups.io to automatically receive all group messages.