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

时间:2017-12-06 14:10:47

标签: ios visual-studio xamarin.ios

我尝试使用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 - &gt;查看 - &gt; TextView的

但是在结果中,TableView messagesList显示为白色矩形。 有人可以帮我解决这个问题吗?

已更新 这里执行OwnerMessageTableViewCell

public partial class OwnerMessageTableViewCell:UITableViewCell     {         public static readonly UINib Nib = UINib.FromName(&#34; OwnerMessageTableViewCell&#34;,NSBundle.MainBundle);         public static readonly NSString Key = new NSString(&#34; OwnerMessageTableViewCell&#34;);         public OwnerMessageTableViewCell(IntPtr句柄):base(句柄)         {         }         public static OwnerMessageTableViewCell Create()         {             return(OwnerMessageTableViewCell)Nib.Instantiate(null,null)[0];         }         internal void BindData(字符串消息,UIImage头像)         {             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;

2 个答案:

答案 0 :(得分:1)

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

本机ios中有几种​​解决方案,我建议您查看Auto-Sizing Row Height

步骤:

  1. 修改Content Hugging Priority上的Content Compression Resistance PriorityUITextView以操作单元格布局

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

答案 1 :(得分:0)

创建一个uiview,它将充当文本视图的容器。在uiview中添加textview。然后将文本视图的颜色设置为透明。设置约束,以便在uiview内正确定位文本视图。将角半径应用于uiview,使其看起来像一个气泡。您需要根据包含的文本调整单元格的高度。