1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/examples/label_example.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/views/border.h"
9 #include "ui/views/controls/label.h"
10 #include "ui/views/layout/box_layout.h"
11 #include "ui/views/view.h"
13 using base::ASCIIToUTF16;
14 using base::WideToUTF16;
21 // A Label with a constrained preferred size to demonstrate eliding or wrapping.
22 class PreferredSizeLabel : public Label {
25 virtual ~PreferredSizeLabel();
28 virtual gfx::Size GetPreferredSize() OVERRIDE;
31 DISALLOW_COPY_AND_ASSIGN(PreferredSizeLabel);
34 PreferredSizeLabel::PreferredSizeLabel() : Label() {
35 SetBorder(Border::CreateSolidBorder(2, SK_ColorCYAN));
38 PreferredSizeLabel::~PreferredSizeLabel() {}
40 gfx::Size PreferredSizeLabel::GetPreferredSize() { return gfx::Size(100, 40); }
44 LabelExample::LabelExample() : ExampleBase("Label") {}
46 LabelExample::~LabelExample() {}
48 void LabelExample::CreateExampleView(View* container) {
49 // A very simple label example, followed by additional helpful examples.
50 container->SetLayoutManager(new BoxLayout(BoxLayout::kVertical, 0, 0, 10));
51 Label* label = new Label(ASCIIToUTF16("Hello world!"));
52 container->AddChildView(label);
54 const wchar_t hello_world_hebrew[] =
55 L"\x5e9\x5dc\x5d5\x5dd \x5d4\x5e2\x5d5\x5dc\x5dd!";
56 label = new Label(WideToUTF16(hello_world_hebrew));
57 label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
58 container->AddChildView(label);
60 label = new Label(WideToUTF16(L"A UTF16 surrogate pair: \x5d0\x5b0"));
61 label->SetHorizontalAlignment(gfx::ALIGN_RIGHT);
62 container->AddChildView(label);
64 label = new Label(ASCIIToUTF16("A left-aligned blue label."));
65 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
66 label->SetEnabledColor(SK_ColorBLUE);
67 container->AddChildView(label);
69 label = new Label(ASCIIToUTF16("A Courier-18 label with a shadow."));
70 label->SetFontList(gfx::FontList("Courier, 18px"));
71 label->SetShadowColors(SK_ColorGRAY, SK_ColorLTGRAY);
72 label->SetShadowOffset(1, 1);
73 container->AddChildView(label);
75 label = new PreferredSizeLabel();
76 label->SetText(ASCIIToUTF16("A long label will elide toward its logical end "
77 "if the text's width exceeds the label's available width."));
78 container->AddChildView(label);
80 label = new PreferredSizeLabel();
81 label->SetText(ASCIIToUTF16("A multi-line label will wrap onto subsequent "
82 "lines if the text's width exceeds the label's available width."));
83 label->SetMultiLine(true);
84 container->AddChildView(label);
87 } // namespace examples