gpt4 book ai didi

ios - 什么是 IOS 中多行文本的最佳文本容器

转载 作者:行者123 更新时间:2023-11-29 00:07:09 24 4
gpt4 key购买 nike

我尝试使用 Xamarin 在 Visual Studio 中制作带有聊天功能的 Android/IOS 应用程序。我对聊天气球有意见。对于 Android 来说没问题,我使用 Text View 作为消息容器。但在 IOS 中我遇到了一个问题 - 如果我使用 TextView 我只能得到白色矩形。为了实现消息列表,我使用 UITableView 和此代码

public override void ViewDidLoad(){
_chatsMessagesListAdapter = new MessagesTableSourceClass(chatData._messages);
messagesList.Source = _chatsMessagesListAdapter;
}

public class MessagesTableSourceClass : UITableViewSource
{
readonly List<MessageData> _dataList;
public MessagesTableSourceClass(List<MessageData> dataList)
{
_dataList = dataList;
}
public override nint RowsInSection(UITableView tableview, nint section)
{
return _dataList.Count;
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
OwnerMessageTableViewCell cell = tableView.DequeueReusableCell(MessageTableViewCell.Key) as OwnerMessageTableViewCell ?? OwnerMessageTableViewCell.Create();
cell.BindData(_dataList[indexPath.Row]._message, _dataList[indexPath.Row]._image);
return cell;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow(indexPath, true);
}
public override UIView GetViewForFooter(UITableView tableView, nint section)
{
return new UIView();
}
}

OwnerMessageTableViewCell.xib是简单的结构TableViewCell -> View -> TextView

但结果 TableView messagesList 显示为白色矩形。谁能帮我解决这个问题?

已更新这里是OwnerMessageTableViewCell的实现

public partial class OwnerMessageTableViewCell : UITableViewCell
{
public static readonly UINib Nib = UINib.FromName("OwnerMessageTableViewCell", NSBundle.MainBundle);
public static readonly NSString Key = new NSString("OwnerMessageTableViewCell");
public OwnerMessageTableViewCell (IntPtr handle) : base (handle)
{
}
public static OwnerMessageTableViewCell Create()
{
return (OwnerMessageTableViewCell)Nib.Instantiate(null, null)[0];
}
internal void BindData(string message, UIImage avatar)
{
messageIcon.Image = avatar;
workerIcon.Hidden = false;
messageText.Text = message;
}
}

这里是xib文件内容

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" id="252" customClass="OwnerMessageTableViewCell" rowHeight="44">
<rect key="frame" x="0.0" y="0.0" width="240" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="252" id="253">
<rect key="frame" x="0.0" y="0.0" width="240" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" id="280" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES" clipsSubviews="YES" image="Images/worker_icon.png">
<rect key="frame" x="190" y="0.0" width="50" height="50"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMinY="YES"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute keyPath="layer.cornerRadius" type="number">
<real key="value" value="25"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</imageView>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="justified" id="307" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES" text="Message Text here..." editable="NO" selectable="NO">
<rect key="frame" x="0.0" y="0.0" width="182" height="50"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" colorSpace="calibratedRGB" red="0.094117647058823528" green="0.31372549019607843" blue="0.63921568627450975" alpha="1"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<inset key="scrollIndicatorInsets" minX="5" minY="5" maxX="5" maxY="5"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute keyPath="layer.cornerRadius" type="number">
<real key="value" value="15"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</textView>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" id="384" translatesAutoresizingMaskIntoConstraints="NO" fixedFrame="YES" image="Images/worker_icon.png">
<rect key="frame" x="220" y="30" width="20" height="20"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
</subviews>
</tableViewCellContentView>
<point key="canvasLocation" x="-132.5" y="-317"/>
<connections>
<outlet property="messageIcon" destination="280" id="name-outlet-280"/>
<outlet property="messageText" destination="307" id="name-outlet-307"/>
<outlet property="workerIcon" destination="384" id="name-outlet-384"/>
</connections>
<color key="backgroundColor" colorSpace="calibratedRGB" red="0.54117647058823526" green="0.54117647058823526" blue="0.54117647058823526" alpha="1"/>
</tableViewCell>
</objects>
<resources>
<image name="chat_bg.jpg" width="414" height="648"/>
</resources>
</document>

已更新

这是 VS 设计器窗口的屏幕

design of tableviewcell ;

properties ;

layout ;

最佳答案

您需要根据文本调整单元格高度。

原生ios有几种解决方案,推荐你看看Auto-Sizing Row Height .

步骤:

  1. 修改Content Hugging PriorityContent Compression Resistance PriorityUITextView 上操作cell布局

  2. 设置Estimated RowHeight启用自动调整大小。

关于ios - 什么是 IOS 中多行文本的最佳文本容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47676307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com