当占位符太长而无法放入 StackLayout 时,我的 Xamarin.forms 应用程序中的 XAML 编辑器没有包装占位符。但是,我真的很想使用编辑器,因为它可以进行多行输入和文本。请让我知道我如何实现这一目标。下面是我的 XAML 代码。
<ScrollView>
<StackLayout>
<Frame BorderColor="LightGray" HasShadow="False" Padding="0">
<Editor x:Name="Editor" MaxLength="450" HeightRequest="200" Placeholder="Long placeholder text, the text should wrap but it doesn't. It just keeps going on and not being visible on the screen"></Editor>
</Frame>
</StackLayout>
</ScrollView>
下面是我尝试使占位符文本为:“这是非常非常长的占位符文本lorem ipsum dolor sat amet hi hi hi hi”时的结果图像。
答案 0 :(得分:0)
该问题是iOS系统设计造成的。如果你确实想让placeHolder在iOS中扭曲,你可以使用Custom Renderer(添加一个标签来显示占位符)
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using App11.iOS;
[assembly:ExportRenderer(typeof(Editor),typeof(MyEditorRenderer))]
namespace App11.iOS
{
public class MyEditorRenderer: EditorRenderer
{
UILabel placeHolderLabel;
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
if(placeHolderLabel==null)
{
var frame = this.Bounds;
placeHolderLabel = new UILabel() {Frame = new CoreGraphics.CGRect(5,-10,380,Element.HeightRequest/2) };
placeHolderLabel.Lines = 0;
placeHolderLabel.LineBreakMode = UILineBreakMode.WordWrap;
// placeHolderLabel.BackgroundColor = UIColor.LightGray;
placeHolderLabel.TextColor = UIColor.LightGray;
UpdateplaceHolder();
NSNotificationCenter.DefaultCenter.AddObserver(this,new ObjCRuntime.Selector("UpdateplaceHolder"),UITextView.TextDidChangeNotification,null);
}
}
}
[Export("UpdateplaceHolder")]
void UpdateplaceHolder()
{
if (Control.Text.Length>0)
{
this.placeHolderLabel.RemoveFromSuperview();
return;
}
this.placeHolderLabel.Font = this.TextView.Font;
this.placeHolderLabel.TextAlignment = this.TextView.TextAlignment;
this.placeHolderLabel.Text = Element.Placeholder;
Element.Placeholder = "";
// this.placeHolderLabel.SizeToFit();
this.AddSubview(placeHolderLabel);
}
}
}
答案 1 :(得分:0)
对此的完整解决方案涉及两件事
对占位符 (UILabel) 的宽度使用 AutoLayout 约束,请参阅此 XF Github Issue for the code
标签包装完成后,您需要确保编辑器容器足够大以允许看到 UILabel。您可以在编辑器上使用 HeightRequest 属性,但这会锁定高度。当文本太长时,锁定高度会导致控件内滚动,当编辑器已经在 ScrollView 中时,这是一个问题。答案是使用 rickclephas
再次覆盖 CustomRenderer (this post) 中的 SizeThatFits 以进行救援