NetNewsWire/iOS/MasterTimeline/Cell/MasterTimelineCellLayout.swift

114 lines
3.0 KiB
Swift
Raw Normal View History

//
// MasterTimelineCellLayout.swift
2019-04-30 00:19:08 +02:00
// NetNewsWire-iOS
//
2019-04-30 00:19:08 +02:00
// Created by Maurice Parker on 4/29/19.
// Copyright © 2019 Ranchero Software. All rights reserved.
//
import UIKit
2019-04-30 00:19:08 +02:00
protocol MasterTimelineCellLayout {
2019-04-29 21:40:14 +02:00
2019-04-30 00:19:08 +02:00
var height: CGFloat {get}
var unreadIndicatorRect: CGRect {get}
var starRect: CGRect {get}
var iconImageRect: CGRect {get}
2019-04-30 00:19:08 +02:00
var titleRect: CGRect {get}
var summaryRect: CGRect {get}
var feedNameRect: CGRect {get}
var dateRect: CGRect {get}
}
2019-04-30 00:19:08 +02:00
extension MasterTimelineCellLayout {
2019-04-29 21:40:14 +02:00
static func rectForUnreadIndicator(_ point: CGPoint) -> CGRect {
var r = CGRect.zero
2019-04-30 00:19:08 +02:00
r.size = CGSize(width: MasterTimelineDefaultCellLayout.unreadCircleDimension, height: MasterTimelineDefaultCellLayout.unreadCircleDimension)
2019-04-29 21:40:14 +02:00
r.origin.x = point.x
r.origin.y = point.y + 5
2019-04-29 21:40:14 +02:00
return r
}
2019-04-30 00:19:08 +02:00
2019-04-29 21:40:14 +02:00
static func rectForStar(_ point: CGPoint) -> CGRect {
var r = CGRect.zero
2019-04-30 00:19:08 +02:00
r.size.width = MasterTimelineDefaultCellLayout.starDimension
r.size.height = MasterTimelineDefaultCellLayout.starDimension
r.origin.x = floor(point.x - ((MasterTimelineDefaultCellLayout.starDimension - MasterTimelineDefaultCellLayout.unreadCircleDimension) / 2.0))
r.origin.y = point.y + 3
2019-04-29 21:40:14 +02:00
return r
}
2019-04-30 00:19:08 +02:00
static func rectForIconView(_ point: CGPoint, iconSize: IconSize) -> CGRect {
2019-04-29 21:40:14 +02:00
var r = CGRect.zero
2019-11-09 00:16:09 +01:00
r.size = iconSize.size
r.origin.x = point.x
r.origin.y = point.y + 4
2019-04-29 21:40:14 +02:00
return r
}
static func rectForTitle(_ cellData: MasterTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> (CGRect, Int) {
var r = CGRect.zero
if cellData.title.isEmpty {
return (r, 0)
}
2019-04-29 21:40:14 +02:00
r.origin = point
2019-04-30 00:19:08 +02:00
let sizeInfo = MultilineUILabelSizer.size(for: cellData.title, font: MasterTimelineDefaultCellLayout.titleFont, numberOfLines: cellData.numberOfLines, width: Int(textAreaWidth))
2019-04-29 21:40:14 +02:00
r.size.width = textAreaWidth
r.size.height = sizeInfo.size.height
if sizeInfo.numberOfLinesUsed < 1 {
r.size.height = 0
}
2019-04-29 21:40:14 +02:00
return (r, sizeInfo.numberOfLinesUsed)
2019-04-29 21:40:14 +02:00
}
2019-04-30 00:19:08 +02:00
2019-04-29 21:40:14 +02:00
static func rectForSummary(_ cellData: MasterTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat, _ linesUsed: Int) -> CGRect {
let linesLeft = cellData.numberOfLines - linesUsed
2019-04-29 21:40:14 +02:00
var r = CGRect.zero
if cellData.summary.isEmpty || linesLeft < 1 {
return r
}
2019-04-29 21:40:14 +02:00
r.origin = point
2019-04-30 00:19:08 +02:00
let sizeInfo = MultilineUILabelSizer.size(for: cellData.summary, font: MasterTimelineDefaultCellLayout.summaryFont, numberOfLines: linesLeft, width: Int(textAreaWidth))
2019-04-29 21:40:14 +02:00
r.size.width = textAreaWidth
r.size.height = sizeInfo.size.height
if sizeInfo.numberOfLinesUsed < 1 {
r.size.height = 0
}
2019-04-29 21:40:14 +02:00
return r
2019-04-29 21:40:14 +02:00
}
2019-04-29 21:40:14 +02:00
static func rectForFeedName(_ cellData: MasterTimelineCellData, _ point: CGPoint, _ textAreaWidth: CGFloat) -> CGRect {
2019-04-30 00:19:08 +02:00
var r = CGRect.zero
2019-04-29 21:40:14 +02:00
r.origin = point
2019-04-30 00:19:08 +02:00
let feedName = cellData.showFeedName == .feed ? cellData.feedName : cellData.byline
let size = SingleLineUILabelSizer.size(for: feedName, font: MasterTimelineDefaultCellLayout.feedNameFont)
2019-04-29 21:40:14 +02:00
r.size = size
if r.size.width > textAreaWidth {
r.size.width = textAreaWidth
}
return r
2019-04-29 21:40:14 +02:00
}
2019-04-29 21:40:14 +02:00
}