How can I add word wrap to EditorGUILayout.TextArea? - c#

How can I add word wrap to the editor textarea? I'm trying to mimic the [TextArea] attribute (word wrap, automatically increase height when needed)
I know that GUILayout.TextArea() works but I was hoping to use EditorGUILayout because according to the docs it correctly responds to copy/pasting, select all, etc.
My code:
obj.talkableFlavorText = EditorGUILayout.TextArea(obj.talkableFlavorText, GUILayout.MinHeight(textAreaHeight));

Use a GUIStyle and set the wordWrap property to true.
Complete example based on Unity Editor Window Example
using UnityEngine;
using UnityEditor;
public class MyWindow : EditorWindow
{
string myString = "Hello World";
// Add menu named "My Window" to the Window menu
[MenuItem("Window/My Window")]
static void Init()
{
// Get existing open window or if none, make a new one:
MyWindow window = (MyWindow)EditorWindow.GetWindow(typeof(MyWindow));
window.Show();
}
void OnGUI()
{
GUIStyle style = new GUIStyle(EditorStyles.textArea);
style.wordWrap = true;
myString = EditorGUILayout.TextArea(myString, style);
}
}
Result:

Related

How can I build three buttons touching horizontally in Unity GUI

I'm trying to build a custom EditorWindow in Unity and I want to have the effect like in the picture with the buttons acting like tabs.
I tried to place the buttons horizontally and have negative spaces between them but is not quite the effect I want. Below is my code.
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Heightmap")) { }
GUILayout.Space(-10);
if (GUILayout.Button("Vegetation")) { }
GUILayout.Space(-10);
if (GUILayout.Button("Details")){ }
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
Use GUILayout.Toolbar:
using UnityEngine;
using UnityEditor;
public class MyWindow : EditorWindow
{
int toolbarInt = 0;
string[] toolbarStrings = {"Heightmap", "Vegetation", "Details"};
// Add menu named "My Window" to the Window menu
[MenuItem("Window/My Window")]
static void Init()
{
// Get existing open window or if none, make a new one:
MyWindow window = (MyWindow)EditorWindow.GetWindow(typeof(MyWindow));
window.Show();
}
void OnGUI()
{
toolbarInt = GUILayout.Toolbar(toolbarInt, toolbarStrings);
switch (toolbarInt)
{
case 0:
GUILayout.Button("Content for Heightmap");
break;
case 1:
GUILayout.Button("Content for Vegetation");
break;
case 2:
GUILayout.Button("Button for Details");
break;
}
}
}

Responsive Design UI with DockPanel Suite

I have design 1 winform to look like the picture. But I want the highlighted yellow part to be dockable with dockpanel suite reference. Is that do-able or any other suggestion of better design?
Right now the treeview is on the dockpanel and the red box part is a usercontrol placed in the same dockpanel. I tried to put the redbox as another form but I can't place it as it is in the picture. Also, this winform is need to be responsive so I put in the redbox part in a table layout panel.winform design and not familiar actually with the dockpanel suite reference. If there is a beginner tutorial that I can refer to, it would be much appreciated.
Current design:
There are two approach to your problem. First is dirty one and second elegant one. By dirty and elegant i mean way they display. Method they work are both same.
I will explain to you how to do it on empty form and you just implement that in your populated one.
First create new form.
Add 2 or more GroupBoxes to it
Add some items inside them (just to see if it works)
At the top of the each boxes add Button which will toggle visibility
Our form now looks like this and let's look of code behind it.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
public partial class TestForm : Form
{
// This is property
bool ShowFirstGroupBox
{
get
{
// We let user get our property from private variable
return _ShowFirstGroupBox;
}
set
{
// When user change this property we do something based on that
switch(value)
{
case true:
groupBox1.Size = new Size(groupBox1.Width, FirstGroupBoxDefaultHeight);
break;
case false:
groupBox1.Size = new Size(groupBox1.Width, 55);
break;
}
_ShowFirstGroupBox = value;
}
}
bool ShowSecondGroupBox
{
get
{
return _ShowSecondGroupBox;
}
set
{
switch (value)
{
case true:
groupBox2.Size = new Size(groupBox1.Width, FirstGroupBoxDefaultHeight);
break;
case false:
groupBox2.Size = new Size(groupBox1.Width, 55);
break;
}
_ShowSecondGroupBox = value;
}
}
// We store our boxes current state ( TRUE = shown, FALSE = HIDDEN )
bool _ShowFirstGroupBox = true;
bool _ShowSecondGroupBox = true;
// We store our default height for groupboxes
int FirstGroupBoxDefaultHeight;
int SecondGroupBoxDefaultHeight;
public TestForm()
{
InitializeComponent();
// Assigning default height of our groupboxes
FirstGroupBoxDefaultHeight = groupBox1.Height;
SecondGroupBoxDefaultHeight = groupBox2.Height;
}
private void button1_Click_1(object sender, EventArgs e)
{
ShowFirstGroupBox = !(_ShowFirstGroupBox); // This sets our property value to opposite of this boolean
}
private void button1_Click_1(object sender, EventArgs e)
{
ShowSecondGroupBox = !(_ShowSecondGroupBox); // This sets our property value to opposite of this boolean
}
}
}
Now when we have code like this and press button it will collapse groupbox.
NOTE: Controls under groupbox are still on place but just hidden since they are child of groupbox and everything outside of bounds is not visible to user.
This is dirty way since i would like to display it much prettier with MINUS sign on the right side of the groupbox title so i do not have button inside it. To do this you would need to create custom control which inherits groupbox, add button to it and position it in title bar and create event for it. It is easy if you have ever tried creating custom controls but if you haven't and you think dirty approach is okay with you then do not try it.

Xamarin clickable substring in custom label

I am trying to make it so users can click a certain substring in a label and it would run a method, for example clicking #hashtag would run OpenHashtag(string hashtagand clicking a #taggedUser would run ViewProfile(taggedUser)
I found this tutorial, except I don't want phone numbers or URLs to be clickable, only hashtags and tagged users.
These are the renders its using
Android
[assembly: ExportRenderer(typeof(BodyLabel), typeof(BodyLabelAndroid))]
namespace SocialNetwork.Droid.Renderers
{
public class BodyLabelAndroid : LabelRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);
var view = (BodyLabel)Element;
if (view == null) return;
TextView textView = new TextView(Forms.Context);
textView.LayoutParameters = new LayoutParams(LayoutParams.WrapContent, LayoutParams.WrapContent);
textView.SetTextColor(view.TextColor.ToAndroid());
// Setting the auto link mask to capture all types of link-able data
textView.AutoLinkMask = MatchOptions.All;
// Make sure to set text after setting the mask
textView.Text = view.Text;
textView.SetTextSize(ComplexUnitType.Dip, (float)view.FontSize);
// overriding Xamarin Forms Label and replace with our native control
SetNativeControl(textView);
}
}
}
IOS
[assembly: ExportRenderer(typeof(BodyLabel), typeof(BodyLabeliOS))]
namespace SocialNetwork.iOS.Renderers
{
public class BodyLabeliOS : ViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
var view = (AwesomeHyperLinkLabel)Element;
if (view == null) return;
UITextView uilabelleftside = new UITextView(new CGRect(0, 0, view.Width, view.Height));
uilabelleftside.Text = view.Text;
uilabelleftside.Font = UIFont.SystemFontOfSize((float)view.FontSize);
uilabelleftside.Editable = false;
uilabelleftside.DataDetectorTypes = UIDataDetectorType.All;
uilabelleftside.BackgroundColor = UIColor.Clear;
SetNativeControl(uilabelleftside);
}
}
}
Android:
Instead of using textView.AutoLinkMask = MatchOptions.All
you can use
Linkify.AddLinks method. Define your regular expression (for example, any word which starts with # or #) and it will work.
But on iOS, it is more complicated I think.
There I see two options:
Use WebView. Parse your string and add "<a href" where needed.
Break your text to pieces and add separate labels for each clickable part. If you want to click only hashtags and tagged users you can add the appropriate labels just below the text. Afterwards you can add tap gesture recognizers to handle the clicks.

SimpleWPFReporting's PDF has no content

I'm trying to generate a very simple PDF just for practice by using SimpleWPFReporting.
Since there is no sample code in its documentation, I tried creating a sample from what I understood:
Window1.xaml.cs
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public class Sample
{
private string _aw;
public string Aw
{
get { return _aw; }
set { _aw = value; }
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
StackPanel stkMain = new StackPanel();
stkMain.Background = Brushes.Pink;
stkMain.Height = 100;
stkMain.Width = 100;
TextBlock txtSample = new TextBlock();
txtSample.Text = "SAMPLE TEXT";
stkMain.Children.Add(txtSample);
ReportOrientation portrait = ReportOrientation.Portrait;
Sample sample = new Sample()
{
Aw = "Some text"
};
Report.ExportReportAsPdf(stkMain, sample, portrait);
}
}
It does save as a PDF, but when I open it, it's blank. What I'm expecting is to see at least the TextBlock there.
I don't really understand the use of data context in the arguments, so I just added a class there. I tried looking for tutorials but failed. Did I miss something or did I create it wrong?
I believe you have to specify the stackpanel orientation as "vertical", as stated in the doc
https://github.com/maximcus/SimpleWPFReporting
After digging in to the source code, it uses a Visual,so you have to add the stackpanel to the visual tree in order to be renderer and then printed.
You can create a previewer window, add the stackpanel and now the pdf will show the content.
I will create a previewer by myself, so I will post when I finish it.

Windows Forms Transparent TextBox C# Existing solutions don't work

I am creating my textbox programmatically in a console application that builds a form window on the fly. I am trying to get Input boxes such as the textbox to show up invisible but still allow the user to input data such as username and password or any other customisation fields I provide. This is for a game launcher and I am attempting to make it NOT look like a windows component.
I have tried some of the solutions on the post below.
Transparency for windows forms textbox
EDIT: As you can see above I have already cited that this does not solve my issue. I do not use the form designer as it has a nasty habit of deleting my code because I presume "It knows better".
The Accepted answer for that does not work for me as I do not use the form designer and InitializeComponent();
Does not work it just tells me that it is not a function of the component.
I have gotten as far as this.
using System.Windows.Forms;
namespace Launcher_Namespace
{
public class TransparentTextBox : TextBox
{
public TransparentTextBox()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
}
}
And in the main body of code that initialises fields
//Initialise Inputs
_username = new TransparentTextBox();
_username.Bounds = new Rectangle(120, 10, 120, 21);
_username.BackColor = Color.Transparent;
_username.BorderStyle = 0;
_username.Visible = false;
But all this has achieved is allow me to set _username.BackColor = Color.Transparent; Without throwing an error. The Input box remains White with no border. I just want to make the background transparent. Even MSDN recomends this solution but It does not work for me. My only solution left is to build a custom Label class that grabs the inputs and reads the key inputs and adds them to the .Text property but I don't want to do this.
The solution in your linked answer works fine. If you're not using the designer it doesn't matter... you can still use the same solution. InitializeComponent() is simply a method that's created by the code generator in the designer file. If you ever want to know what it does to create controls (it can be very informational to have a look) then create a control using the designer and then inspect the .Designer.cs file.
EDIT: It acts a little funny. You can override OnPaint to fix the white background and disappearing text, see below. Not a "finished" implementation, the cursor doesn't seem to know where to go, but this should get you in the right direction.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
for (int i = 0; i < 3; i++)
{
var x = new UserControl1 {Location = new Point(0, i*20)};
this.Controls.Add(x);
}
}
}
public class UserControl1 : TextBox
{
public UserControl1()
{
SetStyle(ControlStyles.SupportsTransparentBackColor |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
BackColor = Color.Transparent;
TextChanged += UserControl2_OnTextChanged;
}
protected override void OnPaint(PaintEventArgs e)
{
var backgroundBrush = new SolidBrush(Color.Transparent);
Graphics g = e.Graphics;
g.FillRectangle(backgroundBrush, 0, 0, this.Width, this.Height);
g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF(0,0), StringFormat.GenericDefault);
}
public void UserControl2_OnTextChanged(object sender, EventArgs e)
{
Invalidate();
}
}
}
When we use SetStyle(ControlStyles.UserPaint,true) Control Border Doesn't paint. I did this in Textbox. My textbox border style is FixedSingle but after using setstyle with UserPaint Textbox border is not drawn. Textbox appears like border is set to None.

Categories