Skip to content

[WIP] Custom fonts support in TextStyle fixes #6 #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sample/mpp-library/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ class App() : BaseApplication() {
factory[LoginScreen.Id.EmailInputId] = FlatInputViewFactory(
textStyle = TextStyle(
size = 16,
color = Color(0x16171AFF)
color = Color(0x16171AFF),
font = MR.fonts.GrenzeGotisch.regular
),
backgroundColor = Color(0xF5F5F5FF)
)
Expand All @@ -310,7 +311,8 @@ class App() : BaseApplication() {
factory[InputWidget.DefaultCategory] = SystemInputViewFactory(
textStyle = TextStyle(
size = 16,
color = Color(0x16171AFF)
color = Color(0x16171AFF),
font = MR.fonts.GrenzeGotisch.regular
)
)
factory[TabsWidget.DefaultCategory] = SystemTabsViewFactory(
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import dev.icerock.moko.widgets.core.ViewFactoryContext
import dev.icerock.moko.widgets.core.style.view.MarginValues
import dev.icerock.moko.widgets.core.style.view.TextStyle
import dev.icerock.moko.widgets.core.style.view.WidgetSize
import dev.icerock.moko.widgets.core.utils.applyTextStyleIfNeeded
import dev.icerock.moko.widgets.core.utils.bind
import dev.icerock.moko.widgets.core.utils.setEventHandler
import kotlinx.cinterop.readValue
Expand All @@ -36,13 +37,8 @@ actual class FlatInputViewFactory actual constructor(
val flatInputField = FlatInputField(frame = CGRectZero.readValue())
val textField = flatInputField.textField()!!

textStyle?.color?.also {
textField.textColor = it.toUIColor()
}
textStyle?.size?.also {
textField.font = UIFont.systemFontOfSize(it.toDouble())
}

textField.applyTextStyleIfNeeded(textStyle)

widget.field.data.bind {
if (textField.text == it) return@bind
textField.text = it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ fun TextView.applyCommonTextStyleIfNeeded(textStyle: TextStyle<*>?) {
if (textStyle == null) return

textStyle.size?.also { textSize = it.toFloat() }
textStyle.font?.also { this.setTypeface( it.getTypeface(context)) }
textStyle.fontStyle?.also { applyFontStyle(it) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

package dev.icerock.moko.widgets.core.style.view

import dev.icerock.moko.resources.FontResource

data class TextStyle<C>(
val size: Int? = null,
val color: C? = null,
val fontStyle: FontStyle? = null
val fontStyle: FontStyle? = null,
val font: FontResource? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import platform.UIKit.UITextView
fun TextStyle<*>.toUIFont(defaultFontSize: Double = 17.0): UIFont? {
val styleSize = size?.toDouble()
val fontStyle = fontStyle
if (fontStyle != null || styleSize != null) {
if(font != null){
val fontSize = styleSize ?: defaultFontSize
return font.uiFont(fontSize)
}
else if (fontStyle != null || styleSize != null) {
val fontSize = styleSize ?: defaultFontSize
return when (fontStyle) {
FontStyle.BOLD -> UIFont.boldSystemFontOfSize(fontSize = fontSize)
Expand Down