Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions Example/STBaseProjectExampleTests/STMarkdownFixTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,47 @@ final class STMarkdownTableViewModelCitationTests: XCTestCase {
@MainActor
final class STMarkdownTableViewCitationTapTests: XCTestCase {

func testCustomHeaderItemsAreVisibleAfterLayout() {
let image = self.makeTestIcon()
let style = STMarkdownStyle(
font: .systemFont(ofSize: 14),
textColor: .label,
lineHeight: 18,
kern: 0,
tableHeaderItems: [
STMarkdownTableHeaderItem(identifier: "custom-copy", image: image) { _ in },
STMarkdownTableHeaderItem(identifier: "custom-fullscreen", image: image) { _ in }
],
tableHeaderButtonWidth: 16
)
let tableView = STMarkdownTableView(style: style)

tableView.frame = CGRect(x: 0, y: 0, width: 320, height: 120)
tableView.layoutIfNeeded()

let buttons = self.headerButtons(in: tableView)
XCTAssertEqual(buttons.map(\.accessibilityIdentifier), ["custom-copy", "custom-fullscreen"])
XCTAssertTrue(buttons.allSatisfy { !$0.frame.isEmpty }, "自定义 headerItems 按钮应有稳定非零布局尺寸")
XCTAssertTrue(buttons.allSatisfy { $0.image(for: .normal) != nil }, "自定义 headerItems 按钮应保留配置的图片")
}

func testReassigningHeaderItemsRemovesOldButtons() {
let image = self.makeTestIcon()
let tableView = STMarkdownTableView(style: .default)
tableView.headerItems = [
STMarkdownTableHeaderItem(identifier: "first", image: image) { _ in },
STMarkdownTableHeaderItem(identifier: "second", image: image) { _ in }
]
tableView.headerItems = [
STMarkdownTableHeaderItem(identifier: "third", image: image) { _ in }
]

tableView.frame = CGRect(x: 0, y: 0, width: 320, height: 120)
tableView.layoutIfNeeded()

XCTAssertEqual(self.headerButtons(in: tableView).map(\.accessibilityIdentifier), ["third"])
}

func testCitationTapCallbackFiredWhenCellSelected() {
let style = STMarkdownStyle.default
let tableView = STMarkdownTableView(style: style)
Expand Down Expand Up @@ -481,4 +522,22 @@ final class STMarkdownTableViewCitationTapTests: XCTestCase {
private func gradientLayer(named name: String, in view: UIView) -> CAGradientLayer? {
view.layer.sublayers?.first(where: { $0.name == name }) as? CAGradientLayer
}

private func headerButtons(in view: UIView) -> [UIButton] {
view.subviews.flatMap { subview -> [UIButton] in
var buttons = self.headerButtons(in: subview)
if let button = subview as? UIButton {
buttons.insert(button, at: 0)
}
return buttons
}
}

private func makeTestIcon() -> UIImage {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 16, height: 16))
return renderer.image { context in
UIColor.black.setFill()
context.fill(CGRect(x: 3, y: 3, width: 10, height: 10))
}
}
}
25 changes: 25 additions & 0 deletions Sources/STMarkdown/Core/STMarkdownStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public struct STMarkdownStyle: @unchecked Sendable {
public var tableHeaderTextColor: UIColor?
public var tableBorderColor: UIColor?
public var tableBackgroundColor: UIColor?
/// 顶部工具条背景色(nil 时回退到 tableBackgroundColor → secondarySystemBackground)
public var tableHeaderBarBackgroundColor: UIColor?
/// 表格行最小高度,同步影响 UICollectionView 布局与 computeSize
public var tableMinimumRowHeight: CGFloat
/// 单元格字体,非 nil 时在 configure 阶段覆盖 attributedContent 中的字体大小(保留粗/斜等修饰)
public var tableFont: UIFont?
/// 工具条按钮项(nil → makeDefaultHeaderItems();外界通过 style 统一分发)
public var tableHeaderItems: [STMarkdownTableHeaderItem]?
/// 工具条按钮触控区宽度
public var tableHeaderButtonWidth: CGFloat
/// 容器圆角蒙版,默认四角全圆
public var tableCornerMask: CACornerMask
public var imagePlaceholderTextColor: UIColor?
public var imagePlaceholderBackgroundColor: UIColor?
public var imagePlaceholderCaptionColor: UIColor?
Expand Down Expand Up @@ -158,6 +170,13 @@ public struct STMarkdownStyle: @unchecked Sendable {
tableHeaderTextColor: UIColor? = nil,
tableBorderColor: UIColor? = nil,
tableBackgroundColor: UIColor? = nil,
tableHeaderBarBackgroundColor: UIColor? = nil,
tableMinimumRowHeight: CGFloat = 35,
tableFont: UIFont? = nil,
tableHeaderItems: [STMarkdownTableHeaderItem]? = nil,
tableHeaderButtonWidth: CGFloat = 30,
tableCornerMask: CACornerMask = [.layerMinXMinYCorner, .layerMaxXMinYCorner,
.layerMinXMaxYCorner, .layerMaxXMaxYCorner],
imagePlaceholderTextColor: UIColor? = nil,
imagePlaceholderBackgroundColor: UIColor? = nil,
imagePlaceholderCaptionColor: UIColor? = nil,
Expand Down Expand Up @@ -223,6 +242,12 @@ public struct STMarkdownStyle: @unchecked Sendable {
self.tableHeaderTextColor = tableHeaderTextColor
self.tableBorderColor = tableBorderColor
self.tableBackgroundColor = tableBackgroundColor
self.tableHeaderBarBackgroundColor = tableHeaderBarBackgroundColor
self.tableMinimumRowHeight = tableMinimumRowHeight
self.tableFont = tableFont
self.tableHeaderItems = tableHeaderItems
self.tableHeaderButtonWidth = tableHeaderButtonWidth
self.tableCornerMask = tableCornerMask
self.imagePlaceholderTextColor = imagePlaceholderTextColor
self.imagePlaceholderBackgroundColor = imagePlaceholderBackgroundColor
self.imagePlaceholderCaptionColor = imagePlaceholderCaptionColor
Expand Down
15 changes: 14 additions & 1 deletion Sources/STMarkdown/Table/STMarkdownTableCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,20 @@ public final class STMarkdownTableCell: UICollectionViewCell {
}

func configure(with cellData: STMarkdownTableCellData, style: STMarkdownStyle) {
self.contentLabel.attributedText = cellData.attributedContent
let displayText: NSAttributedString
if let tableFont = style.tableFont {
let mutable = NSMutableAttributedString(attributedString: cellData.attributedContent)
let fullRange = NSRange(location: 0, length: mutable.length)
mutable.enumerateAttribute(.font, in: fullRange, options: []) { value, range, _ in
if let originalFont = value as? UIFont {
mutable.addAttribute(.font, value: originalFont.withSize(tableFont.pointSize), range: range)
}
}
displayText = mutable
} else {
displayText = cellData.attributedContent
}
self.contentLabel.attributedText = displayText
let bgColor = style.tableBackgroundColor ?? UIColor.secondarySystemBackground
self.contentView.backgroundColor = cellData.role.isHeader
? bgColor.withAlphaComponent(0.92)
Expand Down
Loading
Loading