pref(Texts.kt): Compose Text 支持显示两个图标
This commit is contained in:
parent
8db3645d9f
commit
87b7e8339a
|
|
@ -1,16 +1,24 @@
|
|||
package com.huanchengfly.tieba.post.ui.widgets.compose
|
||||
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.text.InlineTextContent
|
||||
import androidx.compose.material.Icon
|
||||
import androidx.compose.material.LocalContentAlpha
|
||||
import androidx.compose.material.LocalContentColor
|
||||
import androidx.compose.material.LocalTextStyle
|
||||
import androidx.compose.material.Text
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.AccountCircle
|
||||
import androidx.compose.material.icons.rounded.Link
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.takeOrElse
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import androidx.compose.ui.text.Placeholder
|
||||
import androidx.compose.ui.text.PlaceholderVerticalAlign
|
||||
import androidx.compose.ui.text.TextLayoutResult
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
|
|
@ -20,6 +28,11 @@ import androidx.compose.ui.text.style.TextAlign
|
|||
import androidx.compose.ui.text.style.TextDecoration
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.TextUnit
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import com.huanchengfly.tieba.post.R
|
||||
import com.huanchengfly.tieba.post.pxToDp
|
||||
import com.huanchengfly.tieba.post.pxToSp
|
||||
import com.huanchengfly.tieba.post.utils.EmoticonManager
|
||||
import com.huanchengfly.tieba.post.utils.EmoticonManager.getEmoticonHeightPx
|
||||
import com.huanchengfly.tieba.post.utils.EmoticonUtil.emoticonString
|
||||
|
|
@ -108,7 +121,7 @@ fun EmoticonText(
|
|||
val sizePx = getEmoticonHeightPx(mergedStyle)
|
||||
val emoticonInlineContent =
|
||||
remember(sizePx) { EmoticonManager.getEmoticonInlineContent(sizePx) }
|
||||
Text(
|
||||
IconText(
|
||||
text,
|
||||
modifier,
|
||||
color,
|
||||
|
|
@ -127,4 +140,99 @@ fun EmoticonText(
|
|||
onTextLayout,
|
||||
style
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun IconText(
|
||||
text: AnnotatedString,
|
||||
modifier: Modifier = Modifier,
|
||||
color: Color = Color.Unspecified,
|
||||
fontSize: TextUnit = TextUnit.Unspecified,
|
||||
fontStyle: FontStyle? = null,
|
||||
fontWeight: FontWeight? = null,
|
||||
fontFamily: FontFamily? = null,
|
||||
letterSpacing: TextUnit = TextUnit.Unspecified,
|
||||
textDecoration: TextDecoration? = null,
|
||||
textAlign: TextAlign? = null,
|
||||
lineHeight: TextUnit = TextUnit.Unspecified,
|
||||
overflow: TextOverflow = TextOverflow.Clip,
|
||||
softWrap: Boolean = true,
|
||||
maxLines: Int = Int.MAX_VALUE,
|
||||
inlineContent: Map<String, InlineTextContent> = emptyMap(),
|
||||
onTextLayout: (TextLayoutResult) -> Unit = {},
|
||||
style: TextStyle = LocalTextStyle.current
|
||||
) {
|
||||
val textColor = color.takeOrElse {
|
||||
style.color.takeOrElse {
|
||||
LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
|
||||
}
|
||||
}
|
||||
val mergedStyle = style.merge(
|
||||
TextStyle(
|
||||
color = textColor,
|
||||
fontSize = fontSize,
|
||||
fontWeight = fontWeight,
|
||||
textAlign = textAlign,
|
||||
lineHeight = lineHeight,
|
||||
fontFamily = fontFamily,
|
||||
textDecoration = textDecoration,
|
||||
fontStyle = fontStyle,
|
||||
letterSpacing = letterSpacing
|
||||
)
|
||||
)
|
||||
val sizePx = getEmoticonHeightPx(mergedStyle)
|
||||
val sizeSp = sizePx.pxToSp().sp
|
||||
val sizeDp = sizePx.pxToDp().dp
|
||||
val iconInlineContent =
|
||||
remember(sizeSp) {
|
||||
mapOf(
|
||||
"link_icon" to InlineTextContent(
|
||||
placeholder = Placeholder(
|
||||
width = sizeSp,
|
||||
height = sizeSp,
|
||||
placeholderVerticalAlign = PlaceholderVerticalAlign.TextCenter
|
||||
),
|
||||
children = {
|
||||
Icon(
|
||||
Icons.Rounded.Link,
|
||||
contentDescription = stringResource(id = R.string.link),
|
||||
modifier = Modifier.size(sizeDp)
|
||||
)
|
||||
}
|
||||
),
|
||||
"user_icon" to InlineTextContent(
|
||||
placeholder = Placeholder(
|
||||
width = sizeSp,
|
||||
height = sizeSp,
|
||||
placeholderVerticalAlign = PlaceholderVerticalAlign.TextCenter
|
||||
),
|
||||
children = {
|
||||
Icon(
|
||||
Icons.Rounded.AccountCircle,
|
||||
contentDescription = stringResource(id = R.string.user),
|
||||
modifier = Modifier.size(sizeDp)
|
||||
)
|
||||
}
|
||||
),
|
||||
)
|
||||
}
|
||||
Text(
|
||||
text,
|
||||
modifier,
|
||||
color,
|
||||
fontSize,
|
||||
fontStyle,
|
||||
fontWeight,
|
||||
fontFamily,
|
||||
letterSpacing,
|
||||
textDecoration,
|
||||
textAlign,
|
||||
lineHeight,
|
||||
overflow,
|
||||
softWrap,
|
||||
maxLines,
|
||||
iconInlineContent + inlineContent,
|
||||
onTextLayout,
|
||||
style
|
||||
)
|
||||
}
|
||||
Loading…
Reference in New Issue