forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xamarinGH-3106 Implemented LineBreakMode to Button (xamarin#11147)
* Added LineBreakMode in the Button and TestAttributes * Added LineBreakMode implementation on Android platform * Added Issue into the Controls project * Removed unused method * Added UWP support for LineBreakMode * Implemented LineBreakMode on iOS * Update Xamarin.Forms.Core/Button.cs Co-authored-by: Stephane Delcroix <stephane@delcroix.org> Co-authored-by: Stephane Delcroix <stephane@delcroix.org> Co-authored-by: Rui Marinho <me@ruimarinho.net> fixes xamarin#3106
- Loading branch information
Showing
11 changed files
with
273 additions
and
84 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue3106.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Xamarin.Forms.CustomAttributes; | ||
using Xamarin.Forms.Internals; | ||
|
||
namespace Xamarin.Forms.Controls.Issues | ||
{ | ||
[Preserve(AllMembers = true)] | ||
[Issue(IssueTracker.Github, 3106, "Added LineBreakMode on Button")] | ||
public class Issue3106 : TestContentPage | ||
{ | ||
int count; | ||
const string content = "Welcome to Xamarin.Forms! Welcome to Xamarin.Forms! Welcome to Xamarin.Forms! Welcome to Xamarin.Forms!"; | ||
const string content2 = "Now users can set a line break mode to texts on Button, the default value doesn't affect any user."; | ||
|
||
Button mainButton; | ||
Button materialButton; | ||
Label lineBreakModeType; | ||
|
||
protected override void Init() | ||
{ | ||
mainButton = new Button | ||
{ | ||
Text = content, | ||
LineBreakMode = LineBreakMode.WordWrap, | ||
HorizontalOptions = LayoutOptions.CenterAndExpand, | ||
VerticalOptions = LayoutOptions.CenterAndExpand | ||
}; | ||
mainButton.Clicked += MainButton_Clicked; | ||
|
||
materialButton = new Button | ||
{ | ||
Text = content, | ||
LineBreakMode = LineBreakMode.WordWrap, | ||
HorizontalOptions = LayoutOptions.CenterAndExpand, | ||
VerticalOptions = LayoutOptions.CenterAndExpand, | ||
Visual = VisualMarker.Material | ||
}; | ||
materialButton.Clicked += MaterialButton_Clicked; | ||
|
||
lineBreakModeType = new Label | ||
{ | ||
Text = LineBreakMode.WordWrap.ToString(), | ||
VerticalOptions = LayoutOptions.EndAndExpand, | ||
LineBreakMode = LineBreakMode.WordWrap, | ||
}; | ||
var layout = new StackLayout | ||
{ | ||
Children = | ||
{ | ||
new Label | ||
{ | ||
Text = "Press the first button to change the LineBreakMode. Press the second button to change the text", | ||
VerticalOptions = LayoutOptions.StartAndExpand | ||
}, | ||
mainButton, | ||
materialButton, | ||
lineBreakModeType | ||
} | ||
}; | ||
|
||
Content = layout; | ||
} | ||
|
||
void MaterialButton_Clicked(object sender, EventArgs e) | ||
{ | ||
if (materialButton.Text.Equals(content2)) | ||
materialButton.Text = mainButton.Text = content; | ||
else | ||
materialButton.Text = mainButton.Text = content2; | ||
} | ||
|
||
void MainButton_Clicked(object sender, EventArgs e) | ||
{ | ||
materialButton.LineBreakMode = mainButton.LineBreakMode = SelectLineBreakMode(); | ||
lineBreakModeType.Text = mainButton.LineBreakMode.ToString(); | ||
} | ||
|
||
LineBreakMode SelectLineBreakMode() | ||
{ | ||
count++; | ||
switch (count) | ||
{ | ||
case 1: | ||
return LineBreakMode.CharacterWrap; | ||
case 2: | ||
return LineBreakMode.HeadTruncation; | ||
case 3: | ||
return LineBreakMode.MiddleTruncation; | ||
case 4: | ||
return LineBreakMode.NoWrap; | ||
case 5: | ||
return LineBreakMode.TailTruncation; | ||
default: | ||
count = 0; | ||
return LineBreakMode.WordWrap; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Xamarin.Forms.Platform.UAP/Extensions/TextBlockExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
|
||
namespace Xamarin.Forms.Platform.UAP.Extensions | ||
{ | ||
internal static class TextBlockExtensions | ||
{ | ||
public static void UpdateLineBreakMode(this TextBlock textBlock, LineBreakMode lineBreakMode) | ||
{ | ||
if (textBlock == null) | ||
return; | ||
|
||
switch (lineBreakMode) | ||
{ | ||
case LineBreakMode.NoWrap: | ||
textBlock.TextTrimming = TextTrimming.Clip; | ||
textBlock.TextWrapping = TextWrapping.NoWrap; | ||
break; | ||
case LineBreakMode.WordWrap: | ||
textBlock.TextTrimming = TextTrimming.None; | ||
textBlock.TextWrapping = TextWrapping.Wrap; | ||
break; | ||
case LineBreakMode.CharacterWrap: | ||
textBlock.TextTrimming = TextTrimming.WordEllipsis; | ||
textBlock.TextWrapping = TextWrapping.Wrap; | ||
break; | ||
case LineBreakMode.HeadTruncation: | ||
// TODO: This truncates at the end. | ||
textBlock.TextTrimming = TextTrimming.WordEllipsis; | ||
DetermineTruncatedTextWrapping(textBlock); | ||
break; | ||
case LineBreakMode.TailTruncation: | ||
textBlock.TextTrimming = TextTrimming.CharacterEllipsis; | ||
DetermineTruncatedTextWrapping(textBlock); | ||
break; | ||
case LineBreakMode.MiddleTruncation: | ||
// TODO: This truncates at the end. | ||
textBlock.TextTrimming = TextTrimming.WordEllipsis; | ||
DetermineTruncatedTextWrapping(textBlock); | ||
break; | ||
default: | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} | ||
|
||
static void DetermineTruncatedTextWrapping(TextBlock textBlock) => | ||
textBlock.TextWrapping = textBlock.MaxLines > 1 ? TextWrapping.Wrap : TextWrapping.NoWrap; | ||
} | ||
} |
Oops, something went wrong.