hav*_*ak5 2 textview uitableview ios swift exclusionpath
我在 tableView 标题中添加了一个视图,由一个 imageView 和一个 textView 组成。图像视图在顶角左对齐,文本视图在图像视图上延伸到屏幕右侧,如下所示。
textView 可以具有动态内容,并且具有如下设置的排除路径:
let imagePath = UIBezierPath(rect: imageView.frame)
self.textView.textContainer.exclusionPaths = [imagePath]
Run Code Online (Sandbox Code Playgroud)
我已经禁用了 textview 的滚动,并在标题视图中设置了以下约束:
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
}
}
Run Code Online (Sandbox Code Playgroud)
它调整标题的大小。但是,当 textView 的文本少于图像的高度时,视图的大小会增加。
有谁知道为什么会这样?
我对此有一个解决方案,因为我自己刚刚遇到了这个问题:) 你看,我试图做一些非常相似的事情,其中 aUITextView的文本应该避免 aUIImageView在其左侧。我的代码如下:
let ticketProfileExclusionPath = UIBezierPath(roundedRect: ticketProfilePicture.frame, cornerRadius: Constants.ProfilePictureExclusionRadius)
ticketContent.textContainer.exclusionPaths.append(ticketProfileExclusionPath)
Run Code Online (Sandbox Code Playgroud)
我的结果不是很好:
如您所见,该问题依赖于CGRect给定ticketContent UITextView作为其排除路径,因为后者假定给定CGRect已更正为其自己的框架,而不是其超级视图的.
修复非常简单,需要使用自诞生之日起(iOS 2.0)就存在的 API:
let exclusionPathFrame = convert(ticketProfilePicture.frame, to: ticketContent).offsetBy(dx: Constants.SystemMargin, dy: Constants.Zero)
Run Code Online (Sandbox Code Playgroud)
我们在这里所做的是转换UIImageView的帧到UITextView的坐标系统,从而提供从的角度正确的排除路径UITextView。添加的偏移量只是为了对齐 myUITableViewCell的所有三个text UIView。
convert()是一种UIView方法。
如您所见,文本现在ticketProfilePicture UIImageView很好地环绕了。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
1169 次 |
| 最近记录: |