https://kotlinlang.org logo
#compose-ios
Title
# compose-ios
s

Sunil Kumar

09/28/2023, 3:32 AM
Hi, Whats the best way to implement dimensions in compose, For example we can use expect/actual logic, in common we define expect class Dimensions like this.
Copy code
expect Dimensions {
    val buttonHeight: Dp
}
And then from android we can easily use dimensions from android in actual class, But in iOS how to do that in actual class using size classes becoz i dont have that much knowledge about iOS app development, any reference or example will be appreciated
r

Rehan Tumbi

09/28/2023, 4:44 AM
If you are using compose for common ui then dp will work without any issues
1
s

Sunil Kumar

09/29/2023, 3:50 AM
Thnx @Rehan Tumbi, thats fine. But what i mean is that we use size classes in ios so that we can adapt to different screen orientations, Regular , compact etc.., Ho we can do that in compose , FOr example, for android i will use its dimens from android native module so that it looks good on different density devices. Similarily how we can implement size classes by code from native ios module, so that we can use in ios module of compose. How we specify font sizes etc. for different density devices in ios
r

Rehan Tumbi

09/29/2023, 4:26 AM
Copy code
val sizeInPx = with(LocalDensity.current) { 16.dp.toPx() }
try this
s

Sunil Kumar

09/29/2023, 4:53 AM
ok thnx
Actually what i meant was :
Someone suggested me to use
expect/actual
like this:
```expect object Dimensions {
val buttonHeight: Dp
val smallPadding: Dp
}```
and on actual for android - use android's dimens resources. in actual for iOS - use size classes by code. in actual for desktop/web - use constants or some that will resolve your task
Is this possible?
r

Rehan Tumbi

09/29/2023, 5:00 AM
Yes, By expect/actual you can write native logic platform specific code, i have tried this "LocalDensity.current" and it worked fine for my project running smoothly in mobile and tablet
s

Sunil Kumar

09/29/2023, 5:16 AM
But i didnt found any useful resource to implement size classes by code. Ok and coming to LocalDensity.current So with LocalDensity.current, font sizes or other sizes will adjust according to device density, right from dp to px or px to dp ? But in compose we use dp, so how we can adapt only dp sizes according to device density?
r

Rehan Tumbi

09/29/2023, 5:28 AM
In my opinion you need to write logic for conversion using "LocalDensity.current" where you pass your standard "dp" size and that function return dp according to device dimension, like sdp or ssp library doing
2 Views