【问题标题】:How to implement Multi-line/AutoResizing UITextView when new line added in xamarin.ios, similar to SMS-app or Skype app如何在 xamarin.ios 中添加新行时实现 Multi-line/AutoResizing UITextView,类似于 SMS-app 或 Skype app
【发布时间】:2019-07-09 09:04:40
【问题描述】:
我们正在开发 xamarin.ios 中的聊天应用程序,因此我们希望在添加新行时实现自动增长的文本视图,例如 sms 或 Skype 应用程序。如何在 xamarin.ios 中实现这一点。我在这方面做了很多研发并在互联网上进行了很多搜索,但还没有得到任何合适的解决方案。
请指导我解决这个问题。
【问题讨论】:
标签:
c#
xamarin
xamarin.ios
【解决方案1】:
参考以下代码:
CGSize oldSize;
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UITextView textView = new UITextView(new CGRect(20,20,100,30));
oldSize = textView.Frame.Size;
textView.Font = UIFont.SystemFontOfSize(16);
textView.TextColor = UIColor.Black;
textView.Changed += (sender, e) => {
if(textView.Text.Length!=0&&textView.Text!="")
{
CGRect frame = textView.Frame;
double newHeight = Math.Ceiling(textView.SizeThatFits(new CGSize(oldSize.Width,9999999999)).Height);
// update the height if the newHeight larger than the old
if(newHeight >= oldSize.Height)
{
textView.Frame = new CGRect(frame.X, frame.Y, frame.Size.Width, newHeight);
}
}
};
View.AddSubview(textView);
}
【解决方案2】:
您好,我们也为此找到了另一个简单的解决方案。
textview.Changed += (object sender, EventArgs e) =>
{
//Action
var NewHeight = txtMsgToSend.ContentSize.Height;
textview.Frame = new CGRect(x:textview.Frame.X, y: textview.Frame.Y, width:
textview.Frame.Width, height: NewHeight);
}
};