Skip to content

Merge kb-switch-width-542 into production #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions knowledge-base/switch-strecth-expand-resize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Stretch Switch Dynamically to Fit Long Text Labels
description: How to expand the TelerikSwitch automatically, according to its text labels.
type: how-to
page_title: Resize the Switch on the fly, based on longer on/off labels
slug: switch-strecth-expand-resize
position:
tags: switch, stretch, resize, expand, labels
ticketid: 1539464
res_type: kb
---

## Description

How do I stretch the Switch horizontally, in order to see the whole dynamic on/off text labels? The goal is to expand the component and fit the complete text automatically inside the TelerikSwitch dynamically at runtime. Setting the width to a fixed value is not a viable solution.

## Solution

The Switch relies on absolute positioning styles, which require defined dimensions to work properly. However, it is possible to calculate the Switch width dynamically, based on the length of its labels.

>caption Expand the Switch on the fly, depending on the text label length

````CSHTML
<TelerikSwitch Value="@Value"
Width="@Width"
OnLabel="@OnLabel"
OffLabel="@OffLabel"></TelerikSwitch>

@code {
bool Value { get; set; }
string OnLabel { get; set; } = "Long text on";
string OffLabel { get; set; } = "Long text off and some more text";

string Width
{
get
{
return Math.Max(OnLabel.Length, OffLabel.Length) * .8 + "em";
}
}
}
````