In my application, i'm parsing the xml file to get the properties of the uilabel, like color, text, alignment. In this i can convert the string to UIColor, but i search lots of links to convert the string to UITextAlignment. I didn,t get any proper solutions.
Waiting for a Solution. Please help me.
UITextAlignment is nothing but enum, so whatever text you are getting in response say you are getting left, center or right then you can create same enum as UITextAlignment ie
enum {
UITextAlignmentLeft = 0,
UITextAlignmentCenter,
UITextAlignmentRight,
}
typedef NSTextAlignment UITextAlignment;
and use string to get corresponding value and provide to textAlignment.
Note:This in in-built enum you have to create your own enum with left as 0, center as 1 and right as 2.
Related
I am trying to get text value in TMPro Text component without markup tags but haven't found any solutions.
Say, <b><color=red>Hello </color><b> world is the value in TMPro Text, and I just want Hello world in c# script.
Bear in mind that tag <b> and color will change, so I would love to remove tags dynamically, meaning I would like not to replacing each tag by text.replace("<b>", "") kind of things.
Does anyone know how to do it?
I dont know about the option of using HTML on tmp but you can attach the text to your script by create a new variable like that:
[SerializeField] TMP_Text textVar;
then you can drag you tmp game object to the component that include this script
and the you can change the text like that:
textVar.text = "what ever";
or get text like that:
string textString = textVar.text;
for the color you can use
Color color = textVar.color;
You can use TMP_Text.GetParsedText () to get the text after it has been parsed and rich text tags removed.
Alternatively you can also use regular expressions to search a string for rich text tags, and remove them while preserving the rest of the string.
using System.Text.RegularExpressions;
public static string GetString (string str)
{
Regex rich = new Regex (#"<[^>]*>");
if (rich.IsMatch (str))
{
str = rich.Replace (str, string.Empty);
}
return str;
}
I was working with Colors class in Flutter.
Added a buildkey function with input "Color zinc" and the same takes the value like Colors.red, Colors purple etc.
However, if I add the input as "Colors Zinc" the values like Colors.red, Colors.purple throws an error. Why this is so?
The class Colors should match the input class (Colors in this case) I think?enter image description here
It would be helpful if you could attach a screenshot or maybe paste the exact error that you are getting.
But meanwhile as I understood it, It seems like that you are telling this works fine for you:
Expanded buildkey(Color zinc, int buildnumber) { ... }
But you are getting some error with this:
Expanded buildkey(Colors zinc, int buildnumber) { ... }
Which is correct behaviour as Colors.[color] returns a swatch constant which is of type color.
e.g: Color selection = Colors.green[400];
For more info: https://api.flutter.dev/flutter/material/Colors-class.html
May be I found the solution of my issue..
When i looked into the dart file of colors class. I found that,
Colors.green is actually an object of Color class. Code is something like this
Class Colors
Color green = Color(....);
Might be that is the reason thats why it requires it requires color class while initilization of variable and not Colors.
I have a list of customized icons to my app, it comes like below setted as IconData, note codePoint (0xe931).
IconData angry_face = IconData(0xe931, fontFamily: _fontFamily);
There's nothing wrong with that, but I've some cases where I need this icon as Unicode string to be used as a text. It should be done just like:
// It works too
Text('\ue931', style: TextStyle(fontFamily: _fontFamily));
The problem is:
I don't wanna use this code "by hand" because this icons are changed constantly by designers team and sometimes it changes its code messing up my app icons. What I need to do is get the icon object and parse it to the Unicode string, so I can use it with a Text widget.
I thought that would work to get programmatically that code and just use it, but it don't:
var iconcode = iconData.codePoint.toRadixString(16);
var result;
// Error: An escape sequence starting with '\u'
// must be followed by 4 hexadecimal digits or
// from 1 to 6 digits between '{' and '}'
result = '\u$iconcode';
// Just a simple string
result = '\\u$iconcode';
In few words: How can I parse programmatically int codePoint to a valid Unicode string?
Here's the right answer. I tried everything but this... Thank you #julemand101
final result = String.fromCharCode(iconData.codePoint);
I am currently using a simple IntSlider as seen in the code below:
rotAngle = EditorGUILayout.IntSlider("rotation", obj.rotationAngle, -angleRange, angleRange);
I need to implement a more advanced slider that changes one parameter at a time. I need to implement an enum for my slider label. How can this be done?
Let's say you have the label enum
public enum Label
{
A,
B,
C
}
Now you can do something like
int rotAngel;
Label label;
EditorGUILayout.BeginHorizontal();
label = (Label)EditorGUILayout.EnumPopup(label);
rotAngle = EditorGUILayout.IntSlider(obj.rotationAngle, -angleRange, angleRange);
EditorGUILayout.EndHorizontal();
I just added BeginHorizontal and EndHorizontal assuming you want to display both fields next to each other.
Also note that you always have to parse to your enum (Label) because EnumPopup returns an int.
I am developing a Unity 2D game where a user can select a language on the home screen and then the subsequent game should be played in the selected language itself. The language specific changes include audio files, sprites and text on Canvas UI elements. How can I approach this problem?
It depends a bit. There are different approaches for that.
I would probably do it with nested dictionaries and enums but there might be more elegant solutions.
Let's say you have a certain label for each text, sprite, sound, etc. in some enums like
enum TextLabel{
MainPanelTitle,
FireButton,
InfoText,
...
}
enum SpriteLabel{
LanguageFlag,
Background,
...
}
enum SoundLabel{
WelcomeMessage,
NationalHymn,
...
}
And for the language you would also have an enum like
enum Language{
English,
French,
German,
...
}
Than you would setup an individual dictionary for each language like
// Maps text labels to English texts
Dictionary<TextLabel,string> englishTexts;
// Maps text labels to french texts
Dictionary<TextLabel,string> frenchTexts;
// Maps text labels to German texts
Dictionary<TextLabel,string> germanTexts;
...
The same for Sprites (Dictionary<SpriteLabel,Sprite>) and Sounds (Dictionary<SoundLabel, AudioClip>)
You make like a main dictionary for each type for selecting the correct text/image/sound dictionary depending on the language:
Dictionary<Language, Dictionary<TextLabel,string>> texts;
Again the same with sprites and sounds.
Than at some point you have to fill the dictionaries. The texts (sounds, sprites) dictionary is the simplets one since it doesn't depend on external data:
texts = new Dictionary<Language, Dictionary<TextLabel,string>>(){
{Language.English, englishTexts},
{Language.French, frenchTexts},
{Language.German, germanTexts},
...
};
Well, now you "only" have to fill the information somehow into e.g. the text dictionaries. You can do this hardcoded in a script, drag/type in via the inspector or especially for texts e.g. use a .cvs table and parse that in during runtime. There are a lot of options for that step and it probably would fit for another question. In the end all will resume in some lines like
englishTexts[MainPanelTitle] = ...;
englishTexts[FireButton] = ...;
...
frenchTexts[MainPanelTitle] = ...;
...
Once e.g. the text dictionaries are filled with the information you can access a certain text (e.g. InfoText in French ) like
string infotext = texts[Language.French][TextLabel.InfoText];
As I said probably not the most elegant solution but that's how I would do it ;)
I do it before by this track
Var string Language;
Var UItext Title;
Then create buttons to choose language
in English button :
Language = “English”;
Then check what user choose
If (Language == “English”) {
Title = “Title Game” ;
}else if (Language == “Arabic”) {
Title = “عنوان اللعبة" ;
}