我们正在开发xamarin.ios中的聊天应用程序,所以我们想要在添加像sms或Skype app这样的新行时实现自动增长的textview。如何在xamarin.ios中实现这一点。我已经做了很多这方面的研发和许多在互联网上的搜索,但还没有得到任何适当的解决方案。
请指导我解决这个问题。
请参阅以下代码:
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);
}
嗨我们还有另一个简单的解决方案。
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);
}
};