簡體   English   中英

如何在表標題視圖內部使用exclusionPaths正確調整textview的大小

[英]How to properly resize textview with exclusionPaths inside of table header view

我已將視圖添加到由imageView和textView組成的tableView標頭中。 圖像視圖在左上角對齊,文本視圖在圖像視圖上方延伸到屏幕的右側,如下所示。

參考圖片

textView可以具有動態內容,並具有如下設置的排除路徑:

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

我已禁用了textview的滾動,並在標題視圖中設置了以下約束:

TextView:左-8像素,右-8像素,頂-0像素,底-8像素

ImageView:左側-8像素,寬度-100像素,高度100像素,頂部-8像素,底部-大於或等於8像素

在用動態文本填充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的文本少於圖像的高度時,視圖的大小會增加。

三行文字的示例: 在此處輸入圖片說明

六行文字的示例: 在此處輸入圖片說明

足以通過imageview的文本示例: 在此處輸入圖片說明

有人知道為什么會這樣嗎?

我有一個解決方案,因為我自己才遇到此問題:)您看,我正在嘗試做非常相似的事情,其中UITextView的文本應避免UIImageView出現在左側。 我的代碼如下:

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

我的結果不是很好:

在此處輸入圖片說明

正如你所看到的,這個問題依靠CGRect給予ticketContent UITextView為排除路徑,因為后者假設給定CGRect修正為自己的框架,而不是它的父的

修復非常簡單,並且需要使用自時間曙光(iOS 2.0)起就存在的API:

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

我們在這里所做的是將UIImageView的框架轉換為UITextView的坐標系,從而從UITextView的角度提供正確的排除路徑。 添加的偏移量只是為了對齊我的UITableViewCell的所有文本UIView的三個。

convert()是一個UIView方法。

在此處輸入圖片說明

如您所見,文本現在很好地環繞了ticketProfilePicture UIImageView

希望這可以幫助。

You can calculate textview text height with content size.

let tempTextview = UITextView(frame: CGRect(x: 50, y: 50, width: 120, height: 250))
tempTextview.text = "Dynamic Text"
let height = tempTextview.contentSize.height

Add you extra top and bottom padding to this height. 

height = height + padding 

if imageview.frame.size.height > height 
{
  return imageview.frame.size.height 
}
else
{
   return height 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM