Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8901

XAMLとか知らねぇよ!!C#だけでUIElement式エディタ拡張をする入門~style編~

$
0
0

はじめに

こんにちは、アドベントカレンダー3日目担当の避雷です。今回は先日に引き続きUIElementの調査を進めていきます。
今回はUIに必須なstyleの設定です。前回までの知識だとUIは縦に一つずつ並べるしかなかったのですが、それだと実際のUIを作るには表現力不足です。例えばYes/Noボタンなんかは横並びにしたいときと縦並びにしたいときがあったりしますよね。今回はそういったものに対してstyleを変更して対応していきます。

VisualElement.styleを編集する

VisualElement.styleはmargin、padding、borderline、alignment等のUIのビジュアル的な部分を設定する項目です。VisualElementのstyleを設定する方法はいくつかあります。

//初期化と同時に設定VisualElementlabel=newLabel("Hello World! From C#"){style={fontSize=64,color=newStyleColor(Color.magenta)}};//要素ごとに設定VisualElementlabel2=newLabel("Hello World! From C#");label2.style.fontSize=64;label2.style.color=newStyleColor(Color.magenta);

image.png
両方同じ描画結果です。
今回は要素ごとに設定することにします。その方が見やすい気がするので。

基本的な設定の紹介

border系

borderColor

ボーダーラインの色を設定します

VisualElementframe=newVisualElement();frame.style.borderColor=newStyleColor(Color.red);

borderWidth

ボーダーラインの太さを設定します

VisualElementframe=newVisualElement();frame.style.borderColor=newStyleColor(Color.red);frame.style.borderBottomWidth=5;frame.style.borderTopWidth=5;frame.style.borderRightWidth=5;frame.style.borderLeftWidth=5;

image.png

borderRadius

ボーダーラインの角の丸さを設定します

frame.style.borderBottomLeftRadius=15;frame.style.borderBottomRightRadius=15;frame.style.borderTopLeftRadius=15;frame.style.borderTopRightRadius=15;

image.png

padding margin系

padding

パディングを設定します(ボーダーラインの内側)

frame.style.paddingTop=10;frame.style.paddingBottom=10;

image.png

margin

マージンを設定します(ボーダーラインの外側)

frame.style.paddingTop=10;frame.style.paddingBottom=10;frame.style.marginRight=10;frame.style.marginLeft=10;

image.png

整列方向系

Flex

子要素の整列方向を設定します

frame.style.flexDirection=newStyleEnum<FlexDirection>(FlexDirection.Column);frame.style.flexDirection=newStyleEnum<FlexDirection>(FlexDirection.Row);

↓Column
image.png

↓Row
image.png

文字系

FontSize

フォントサイズを設定します

Labellabel3=newLabel("ABCDEFG");label3.style.fontSize=32;root.Add(label3);

image.png

Color

文字色を設定します

label3.style.color=newStyleColor(Color.green);

image.png

BackgroundColor

背景色を設定

label3.style.backgroundColor=newStyleColor(Color.magenta);

image.png

先ほどのVisualElementに同様の設定をすると
image.png

こんな感じです。

UnityTextAlign

テキストの基準位置を設定

label3.style.unityTextAlign=newStyleEnum<TextAnchor>(TextAnchor.LowerRight);

image.png

因みにTextFieldの入力を左上からにするには
unityTextAlign無し
image.png

foreach(varchildintextField.Children()){//子オブジェクトに左上整列を指示child.style.unityTextAlign=newStyleEnum<TextAnchor>(TextAnchor.UpperLeft);}

とすると
image.png

みたいになる。multilineなら設定必須…?なお現状だと日本語入力が出来ないみたいです(IMEが反応しない)

textField.isPasswordField

trueにするとパスワード用のインプットフィールドになります

textField.isPasswordField=true;

image.png

おわりに

なんか諦めてUSSとかUMLとか使った方が楽な気がしてきました…C#でもデザインまで出来るということは証明できたのでヨシとしましょう。


Viewing all articles
Browse latest Browse all 8901

Trending Articles