Skip to content

Commit

Permalink
TableViewHeaderFooterView should not override any font or color provi…
Browse files Browse the repository at this point in the history
…ded as part of the AttributedString (microsoft#1929)

* Update CommandBarButton.swift

* Update CommandBarButton.swift

* Command handled event should pass the event source in the handler

* Fix spacing

* Fix lint errors and make updates for some review comments

* Do not override font or color provided in the attributed string

* Fix spacing

* Fix update of titleView font

* Updates for PR feedback
  • Loading branch information
owenconnolly authored and nodes11 committed May 30, 2024
1 parent 2d1a4c0 commit 0fc5403
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions ios/FluentUI/Table View/TableViewHeaderFooterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ open class TableViewHeaderFooterView: UITableViewHeaderFooterView, TokenizedCont
/// - accessoryButtonTitle: Optional accessory button title string.
/// - leadingView: An optional custom view that appears near the leading edge of the view.
@objc open func setup(style: Style, title: String, accessoryButtonTitle: String = "", leadingView: UIView? = nil) {
resolvedTitleFont = tokenSet[.textFont].uiFont
resolvedTitleColor = tokenSet[.textColor].uiColor
titleView.attributedText = NSAttributedString(string: " ") // to clear attributes
titleView.text = title
titleView.isSelectable = false
Expand All @@ -277,6 +279,10 @@ open class TableViewHeaderFooterView: UITableViewHeaderFooterView, TokenizedCont
titleView.attributedText = attributedTitle
titleView.isSelectable = true

let attributes = attributedTitle.attributes(at: 0, effectiveRange: nil)
resolvedTitleFont = attributes[NSAttributedString.Key.font] as? UIFont ?? tokenSet[.textFont].uiFont
resolvedTitleColor = attributes[NSAttributedString.Key.foregroundColor] as? UIColor ?? tokenSet[.textColor].uiColor

setup(style: style, accessoryButtonTitle: accessoryButtonTitle, leadingView: leadingView)
}

Expand Down Expand Up @@ -436,22 +442,23 @@ open class TableViewHeaderFooterView: UITableViewHeaderFooterView, TokenizedCont

private func updateTitleViewFont() {
if let window = window {
let titleFont = tokenSet[.textFont].uiFont
titleView.font = titleFont
// offset text container to center its content
titleView.font = resolvedTitleFont
if let resolvedTitleFont = resolvedTitleFont {
// offset text container to center its content
#if os(iOS)
let scale = window.rootViewController?.view.contentScaleFactor ?? window.screen.scale
let scale = window.rootViewController?.view.contentScaleFactor ?? window.screen.scale
#elseif os(visionOS)
let scale: CGFloat = 2.0
let scale: CGFloat = 2.0
#endif // os(visionOS)
let offset = (floor((abs(titleFont.leading) / 2) * scale) / scale) / 2
titleView.textContainerInset.top = offset
titleView.textContainerInset.bottom = -offset
let offset = (floor((abs(resolvedTitleFont.leading) / 2) * scale) / scale) / 2
titleView.textContainerInset.top = offset
titleView.textContainerInset.bottom = -offset
}
}
}

private func updateTitleAndBackgroundColors() {
titleView.textColor = tokenSet[.textColor].uiColor
titleView.textColor = resolvedTitleColor

if tableViewCellStyle == .grouped {
backgroundView?.backgroundColor = tokenSet[.backgroundColorGrouped].uiColor
Expand All @@ -461,7 +468,7 @@ open class TableViewHeaderFooterView: UITableViewHeaderFooterView, TokenizedCont
backgroundView?.backgroundColor = .clear
}

titleView.font = tokenSet[.textFont].uiFont
titleView.font = resolvedTitleFont
titleView.linkColor = tokenSet[.linkTextColor].uiColor
}

Expand Down Expand Up @@ -500,6 +507,9 @@ open class TableViewHeaderFooterView: UITableViewHeaderFooterView, TokenizedCont
@objc private func handleHeaderViewTapped() {
onHeaderViewTapped?()
}

private var resolvedTitleFont: UIFont?
private var resolvedTitleColor: UIColor?
}

// MARK: - TableViewHeaderFooterView: UITextViewDelegate
Expand Down

0 comments on commit 0fc5403

Please sign in to comment.