ios - 如何使用表头 View 中的 exclusionPaths 正确调整 textview 的大小

标签 ios swift uitableview textview exclusionpath

我在我的 tableView header 中添加了一个 View ,其中包含一个 imageView 和一个 textView。 ImageView 在顶角左对齐, TextView 在 ImageView 上延伸到屏幕右侧,如下所示。

references image

textView 可以有动态内容并且有一个排除路径设置如下:

let imagePath = UIBezierPath(rect: imageView.frame)
self.textView.textContainer.exclusionPaths = [imagePath]

我已禁用 TextView 的滚动并在标题 View 中设置了以下约束:

TextView:左 - 8px,右 - 8px,上 - 0px,下 - 8px

ImageView: left - 8px, width - 100px, height 100px, top - 8px, bottom - 大于等于8px

在我的 textView 填充了动态文本后,我添加了这段代码:

if let headerView = self.tableView.tableHeaderView {
    let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
    var headerFrame = headerView.frame

    if height != headerFrame.size.height {
        headerFrame.size.height = height
        headerView.frame = headerFrame
        self.tableView.tableHeaderView = headerView
    }
}

调整标题的大小。但是,当 textView 的文本少于图像的高度时, View 的大小会增加。

三行文本示例: enter image description here

六行文本示例: enter image description here

足以传递 imageview 的文本示例: enter image description here

有人知道为什么会这样吗?

最佳答案

我有一个解决办法,因为我自己也遇到过这个问题:) 你看,我正在尝试做一些非常相似的事情,其中​​ UITextView的文本应避免 UIImageView在它的左边。我的代码如下:

let ticketProfileExclusionPath = UIBezierPath(roundedRect: ticketProfilePicture.frame, cornerRadius: Constants.ProfilePictureExclusionRadius)
ticketContent.textContainer.exclusionPaths.append(ticketProfileExclusionPath)

我的结果不是很好:

enter image description here

如您所见,问题依赖于 CGRect给了ticketContent UITextView作为它的排除路径,因为 后者假设给定的 CGRect被更正为它自己的框架,而不是它的 super View 的

修复非常简单,需要使用从时间开始就存在的 API (iOS 2.0):

let exclusionPathFrame = convert(ticketProfilePicture.frame, to: ticketContent).offsetBy(dx: Constants.SystemMargin, dy: Constants.Zero)

我们在这里所做的是转换 UIImageView的框架到 UITextView的坐标系,因此从 UITextView 的角度提供正确的排除路径.添加的偏移量只是为了对齐我的所有三个 UITableViewCell的正文 UIView秒。

convert()UIView方法。

enter image description here

如您所见,文本现在环绕在 ticketProfilePicture 周围UIImageView非常好。

希望这对您有所帮助。

关于ios - 如何使用表头 View 中的 exclusionPaths 正确调整 textview 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41845443/

相关文章:

ios - 如何使用邮件邀请参加 iCloud 日历事件,iOS swift

Swift Firebase getIDToken() 使用 Google 凭据崩溃

ios - 设置分组 UITableView 中选定 UITableViewCell 的背景

ios - 使用 UITableViewAutomaticDimension 更新 UITableViewCell 后出现抖动滚动

ios - 第三方应用程序可以阻止其他IOS应用程序被打开或使用吗?

ios - swift |强制取消执行代码

ios - Xcode 10.2 无法在 iOS < 10 的模拟器上运行应用程序

ios - 如何从主页选项卡中的按钮单击按钮时加载的 UiTableViewController 返回 UiTabBar 主页?

regex - swift 正则表达式不起作用

iOS UITableViewCell 需要按两次才能调用 didSelectRowAtIndexPath