Xamarin表单-换行按钮文本 [英] Xamarin Forms - Wrap Button Text

查看:542
本文介绍了Xamarin表单-换行按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

我正在尝试制作一个由按钮网格组成的应用程序.其中一些文本的宽度大于其自身宽度

I'm trying to make an app consisting of a grid of buttons. Some of them have a text larger than it's own width

问题:

问题是XF Button正在包装"文本,中间用省略号(...)替换了一些字符:

The thing is that XF Button is "wrapping" the text replacing some characters in the middle with ellipsis (...):

[

我希望将文本像标签"中的文本一样包裹在多行中:

I would like the text to be wrapped in multiple lines like the text in Labels:

浏览互联网时,我发现了一些方法:

Surfing the internet I found some approaches:

  • 在RelativeLayout中使用按钮和标签. Label与Button重叠,并将InputTransparent属性设置为true,即可实现类似Button的控件.

  • Using a Button and a Label inside a RelativeLayout. The Label overlaps the Button and setting the InputTransparent property to true you can achieve the Button-like control.

在布局中使用标签并处理点击手势

Using a Label inside a Layout and handling the Tap Gestures

使用特定于平台的代码通过CustomRenderer渲染本机按钮(我不希望没有特定于平台的代码来显示按钮)

Using platform-specific code to render the native buttons with a CustomRenderer (I would prefer not to have platform-specific code to show buttons)

这些方法的问题在于结果看起来不像本机按钮,也不像本机按钮那样.

The problem with those approaches is that the result doesn't look like a native button or doesn't behave like a native button.

有一种方法可以同时兼顾两者吗?外观和行为类似于本机按钮的控件.

There is an approach that can give both things? A control that looks and behaves like a native button.

推荐答案

使用特定于平台的代码通过CustomRenderer渲染本机按钮(我不希望没有特定于平台的代码来显示按钮)

Using platform-specific code to render the native buttons with a CustomRenderer (I would prefer not to have platform-specific code for showing the buttons)

这是IMO的最佳解决方案.它将为您提供本机按钮的外观.

This is the best solution IMO. It will give you the look and feel of a native button.

由于表单"按钮没有公开自动换行属性,因此您必须使用本机控件的属性来执行此操作.这是用于iOS上的自定义渲染器以启用自动换行的非常简单的代码:

Since a Forms button does not expose a word wrap property, then you have to use the properties of the native control to do this. Here is the very simple code for a custom renderer on iOS to enable word wrapping:

using System;
using NameSpaceForMyiOSApp;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(Button), typeof(MyButtonRenderer))]
namespace NameSpaceForMyiOSApp
{
    public class MyButtonRenderer : ButtonRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
        {
            base.OnElementChanged(e);
            if (Control != null)
                Control.TitleLabel.LineBreakMode = UIKit.UILineBreakMode.WordWrap;
        }
    }
}

在Android上,文本默认情况下会自动换行,因此不需要自定义渲染器,但是请注意,如果单个单词要长到适合按钮的宽度,则单词本身将被拆分.我在这里可以想到的唯一解决方案是确保按钮宽度足够宽以容纳按钮文本中的最长单词.请参阅此SO答案以获取有关以下内容的更多信息:在空白处优雅地将按钮中的文本换行

On Android, the text does wrap by default, so no custom renderer needed, but note that if a single word is to long to fit in the button's width the word itself will be split. The only solution I can think of here is to make sure your button width is wide enough to hold the longest word in the button text. See this SO answer for more info on this: Wrap text in button gracefully at whitespace

这篇关于Xamarin表单-换行按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆