I am trying to obfuscate some text with Jetpack Compose. Obviously, the blur modifier works wonders for this use case if you have Android 12.
My alternative for devices running a lower API version would be to simply draw a rectangle with a black colour over the text. I assumed this would be relatively easy with existing modifiers like graphicsLayer or drawBehind but I haven't been able to figure it out and I'm at a loss for ideas right now...
My current text composable looks like this:
Text(
modifier = if (blurText) {
Modifier.blur(16.dp, BlurredEdgeTreatment.Unbounded)
} else {
Modifier
},
text = textToObfuscate,
fontFamily = latoFontFamily,
fontWeight = FontWeight.W700,
fontSize = 16.sp,
color = black,
)
I could wrap the text in a Box and have another Box inside it to draw over the Text but that just seems useless and more work than should be necessary.
If anyone has any ideas on how to achieve this simply using a Modifier extension, that would be amazing!
You can use Modifier.drawWithContent as
Text(
modifier = if (blurText) {
Modifier.blur(16.dp, BlurredEdgeTreatment.Unbounded)
} else {
Modifier.drawWithContent {
drawContent()
drawRect(Color.Black)
}
},
text = "textToObfuscate",
fontWeight = FontWeight.W700,
fontSize = 16.sp,
)
Related
I just wanted to ask, if there is any way of making an icon in Android Studio that is rounded and filled or rounded and outlined at the same time. Because, I like the rounded icons more but if I add a rounded icon, I can't make it outlined. I also don't want to import an svg.
Thank you for your answer!
OutlinedButton(
onClick = { Toast.makeText(mContext, "This is a Circular Button with a Icon", Toast.LENGTH_LONG).show() },
modifier = Modifier.size(100.dp),
shape = CircleShape,
border = BorderStroke(5.dp, Color(0XFF0F9D58)),
contentPadding = PaddingValues(0.dp),
colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.Blue)
) {
// Adding an Icon "Add" inside the Button
Icon(Icons.Default.Add, contentDescription = "content description", tint = Color(0XFF0F9D58))
}
More in here...
in before xml in TextView we have
holder.binding.iconInfo.text = "&#x${iconValue}"
and set out icon font to it with typeface dose anyone can help same thing but with jetpack compose
The specific syntax you mention that can be used with TextViews is due to the fact that XML is used when defining layouts. That is an XML numeric character entity that specifies a character with its unicode value in hexadecimal notation.
So it is not related to TextViews, layouts, fonts or icons.
If you want to do the same with a String in Kotlin and the character value is dynamic, you can do something like this
val text = "${Char(iconValue)}"
In Compose a custom font can be defined either as part of your Theme -> Typography and then used as text style or you can define it directly like this
val fontFamily = FontFamily(
// If you have the font in resources
Font(R.font.my_font_normal, weight = FontWeight.Normal),
Font(R.font.my_font_bold, weight = FontWeight.Bold),
Font(R.font.my_font_italic, weight = FontWeight.Normal, style = FontStyle.Italic),
// If you are loading the font at runtime from a file
Font(File(...), weight = FontWeight.Normal),
// ...
)
// Use with Text composable
Text(text = text, fontFamily = fontFamily)
Has anyone found a way to align the string in a Text in a justified way?
In my reading app I'd like to offer the user to choose the justified alignment, where both the .leading both the .trailing alignments are true at the same time. Unfortunately the .multilineTextAlignment modifier for the Text has only three options: .leading, .center and .trailing.
If not I can still wrap an UILabel into a UIViewRepresentable since the UILabel has the .justified alignment option, but I thought it should be available in the Text as well and I was not able to find it.
I know I'm late but I faced same issue , I will write it here, it could help other developer in future
Unfortunately until now Apple doesn't suppourt .justified in SwiftUI 2.0
you need to depend on UIKit
Add this code in your project
struct LabelAlignment: UIViewRepresentable {
var text: String
var textAlignmentStyle : TextAlignmentStyle
var width: CGFloat
func makeUIView(context: Context) -> UILabel {
let label = UILabel()
label.textAlignment = NSTextAlignment(rawValue: textAlignmentStyle.rawValue)!
label.numberOfLines = 0
label.preferredMaxLayoutWidth = width
label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentHuggingPriority(.required, for: .vertical)
return label
}
func updateUIView(_ uiView: UILabel, context: Context) {
uiView.text = text
}
}
enum TextAlignmentStyle : Int{
case left = 0 ,center = 1 , right = 2 ,justified = 3 ,natural = 4
}
Then use it like this
LabelAlignment(text: "Your text here", textAlignmentStyle: .justified, width: UIScreen.main.bounds.width - 20)
change 20 to any number you like to add space on left and right
my solution will will depend on text content size
if the text is small the view will be small and vice versa
so it will act as Text()
When I'm using (Colors.amberAccent) in Flutter framework, the color square appears automatically. How can I show it in Android studio beside Lines number if I'm using Color.fromARGB(255, 100, 100, 23)?
The second question is, can I use this color style (#ff0000) in Flutter framework?
I've put an image to clear my idea.colors
String color = '#ff0000';
String hex = color.replaceAll("#", "");
Color col = Color(int.parse(hex, radix: 16)).withOpacity(1.0);
P.S. Or, you can use this
To answer your first question: It is not possible to display the color in the IDE when you use something else that Colors.colorName.
For your second question: you can use the style you described with this syntax Color(0xff5600). That will return a Color object instance
return new MaterialApp(
title: appTitle,
theme: new ThemeData(
primarySwatch: : Color(0xff5600),
),
home: ...
);
The color square will appear if you put the color as a constant:
Color color = const Color(0xFF536DFE);
I have a custom ViewCell with a StackLayout like this:
var viewLayout = new StackLayout () {
Padding = new Thickness (10, 0, 10, 0),
Orientation = StackOrientation.Horizontal,
Children = { nameLabel, button1, button2 }
};
When I use Xamarin.Forms standard ViewCells, i.e "TextCell", the label will get a blueish color on Android. My questions is: Where can I find this property? I want to set it to my custom nameLabel like this:
nameLabel.TextColor = TextCell.StandardTextColorForCurrentPlatform;
For TextCells:-
On Android you can get this value via Color.Accent.
On WindowsPhone use the system-defined PhoneForegroundBrush.
iOS appears to be hard coded to black.
You can write a simple DependencyService to return the appropriate color per the platform your executing on.