【问题标题】:How can you change the Header View title font on FSCalendar and add a Subtitle?如何更改 FSCalendar 上的 Header View 标题字体并添加字幕?
【发布时间】:2021-08-12 21:43:35
【问题描述】:

我正在尝试将标题标题字体设置为更大的样式字体,更像是大横幅,并在其下方设置年度副标题。

extension FSCalendar {
func customizeCalendar() {
    appearance.caseOptions = [.headerUsesUpperCase]
    appearance.headerDateFormat = "MMM"
    headerHeight = 100
    
    let header = FSCalendarHeaderView()
    header.largeContentTitle?.append("aldjsf")
   
    
    appearance.headerTitleFont = UIFont(name: "SFProDisplay-Bold", size: 200)
    appearance.headerTitleColor = COLOR_BLACK

    appearance.headerTitleOffset = CGPoint(x: 0, y: 0)
    appearance.headerMinimumDissolvedAlpha = 0.6
    
    appearance.todayColor = COLOR_PRIMARY
    appearance.todaySelectionColor = COLOR_BLACK
    
    appearance.titleFont = UIFont(name: "SFProDisplay-Bold", size: 11)
    appearance.titleSelectionColor = COLOR_BLACK
    
    appearance.weekdayFont = UIFont(name: "SFProText-Semibold", size: 11)
    appearance.weekdayTextColor = COLOR_GREY
    appearance.eventDefaultColor = COLOR_BLACK
    
    appearance.subtitleFont = UIFont(name: "SFProDisplay-Bold", size: 20)
   
    
    appearance.selectionColor = COLOR_BLACK
    
    
}

}

即使我正在访问属性 .headerTitleFont 它什么也没做?我尝试过各种尺寸。任何帮助表示赞赏,谢谢。

【问题讨论】:

    标签: swift fscalendar


    【解决方案1】:

    字体大小问题

    您提到的字体似乎在模拟器/设备中不可用,因此它默认为字体。我使用预装的字体尝试了您的方法,并且 headerTitle 按预期进行了更改。预装列表fonts

    appearance.headerTitleFont = UIFont(name: "Noteworthy Light", size: 60)
    

    上面的字体是这样的

    为 headerView 添加字幕

    使用可用的 API,您似乎无法在 headerView 中设置字幕。但或者,您可以通过自定义FSCalendarHeaderCell.titleLabel.attributedText 来实现它,如下所示。请注意,下面的代码仅更改collectionView.visibleCells 的文本,因此您还必须在滚动headerView 时执行此代码

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        for cell in calendar.calendarHeaderView.collectionView.visibleCells {
            //create an attributedString with two lines and different font sizes
            let attributedString = NSMutableAttributedString(string: "Sep\n2021\n")
            
            let attributes0: [NSAttributedString.Key : Any] = [
                .foregroundColor: UIColor.yellow,
                .font: UIFont(name: "HelveticaNeue", size: 40)!
            ]
            let attributes1: [NSAttributedString.Key : Any] = [
                .foregroundColor: UIColor.systemGray2
            ]
            
            attributedString.addAttributes(attributes0, range: NSRange(location: 0, length: 3))
            attributedString.addAttributes(attributes1, range: NSRange(location: 4, length: 4))
            
            //replace titleLabel attributedText with the one we created
            (cell as! FSCalendarHeaderCell).titleLabel.attributedText = attributedString
        }
    }
    

    这是更改FSCalendarHeaderCell.titleLabelattributedText后的样子

    【讨论】:

    • 这太棒了!我最终设置了 headerHeight = 0 并从头开始制作整个东西。但这从长远来看会更好,我现在要更新这个。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 2012-08-28
    • 2015-10-04
    • 1970-01-01
    • 2011-05-02
    相关资源
    最近更新 更多