c# - 在 xamarin.ios 中添加新行时如何实现 Multi-line/AutoResizing UITextView,类似于 SMS-app 或 Skype 应用程序
问题描述
我们正在开发 xamarin.ios 中的聊天应用程序,因此我们希望在添加新行时实现自动增长的文本视图,例如 sms 或 Skype 应用程序。如何在 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);
}
推荐阅读
- gcc - 为 Cortex M3、M4、M23 和 M33 编译单个静态库
- javascript - 使用 .map 复制表(React)
- regex - 如何使用正则表达式替换单元格中的所有特殊字符,但 Google 表格中的空格和破折号除外?
- sql - BigQuery:仅将新值附加到表中
- drupal - 如何在 Drupal 中显示过滤结果的过滤条件?
- mongodb - 如何通过同一文档中的另一个字段 $inc 一个字段?
- amazon-s3 - Amplify add storage - 允许未经身份验证的用户需要进行身份验证配置,但未正确配置
- python - 尝试从 Selenium Python 中的 div 类获取文本时出现 InvalidSelectorException 错误
- postgresql - 如何从 json 表中创建具有多列的新表
- c# - 如何将 C# 控制台应用程序窗口移动到屏幕中心