Skip to content

Commit e54a67f

Browse files
authored
Merge pull request #370 from abbassabeti/ios_18_style
iOS 18 Styling - Event #368
2 parents 447a233 + 9dc99b8 commit e54a67f

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

CalendarKitDemo/CalendarApp/CustomCalendarExampleController.swift

+43-4
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,53 @@ final class CustomCalendarExampleController: DayViewController {
8787
let duration = Int.random(in: 60 ... 160)
8888
event.dateInterval = DateInterval(start: workingDate, duration: TimeInterval(duration * 60))
8989

90-
var info = data.randomElement() ?? []
90+
let info = data.randomElement() ?? []
91+
let attributedInfo = NSMutableAttributedString(
92+
string: info.reduce("", { $0 + $1 + "\n" }),
93+
attributes: [.font: UIFont.systemFont(ofSize: 12, weight: .semibold)]
94+
)
9195

9296
let timezone = dayView.calendar.timeZone
9397
print(timezone)
9498

95-
info.append(dateIntervalFormatter.string(from: event.dateInterval.start, to: event.dateInterval.end))
96-
event.text = info.reduce("", {$0 + $1 + "\n"})
97-
event.color = colors.randomElement() ?? .red
99+
let durationText = dateIntervalFormatter.string(
100+
from: event.dateInterval.start,
101+
to: event.dateInterval.end
102+
)
103+
104+
let color = colors.randomElement() ?? .red
105+
106+
let durationAttributedText: NSMutableAttributedString = {
107+
var attributedString: NSMutableAttributedString!
108+
if #available(iOS 13.0, *) {
109+
let clockIcon = NSTextAttachment()
110+
let config = UIImage.SymbolConfiguration(pointSize: 12, weight: .thin, scale: .small)
111+
let image = UIImage(systemName: "clock", withConfiguration: config)
112+
clockIcon.image = image
113+
let text = NSMutableAttributedString(attachment: clockIcon)
114+
attributedString = text
115+
} else {
116+
attributedString = NSMutableAttributedString(string: "")
117+
}
118+
attributedString.addAttribute(
119+
.paragraphStyle,
120+
value: {
121+
let paragraphStyle = NSMutableParagraphStyle()
122+
paragraphStyle.paragraphSpacingBefore = 5
123+
return paragraphStyle
124+
}(),
125+
range: NSRange(location: 0, length: attributedString.string.count)
126+
)
127+
return attributedString
128+
}()
129+
let attributedDuration = NSAttributedString(
130+
string: String(format: " %@", durationText),
131+
attributes: [.font: UIFont.systemFont(ofSize: 11, weight: .light)]
132+
)
133+
durationAttributedText.append(attributedDuration)
134+
attributedInfo.append(durationAttributedText)
135+
event.attributedText = attributedInfo
136+
event.color = color
98137
event.isAllDay = Bool.random()
99138
event.lineBreakMode = .byTruncatingTail
100139

Sources/Timeline/EventView.swift

+11-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ open class EventView: UIView {
1313
view.isUserInteractionEnabled = false
1414
view.backgroundColor = .clear
1515
view.isScrollEnabled = false
16+
view.clipsToBounds = true
1617
return view
1718
}()
1819

@@ -44,6 +45,7 @@ open class EventView: UIView {
4445
public func updateWithDescriptor(event: EventDescriptor) {
4546
if let attributedText = event.attributedText {
4647
textView.attributedText = attributedText
48+
textView.setNeedsLayout()
4749
} else {
4850
textView.text = event.text
4951
textView.textColor = event.textColor
@@ -53,7 +55,9 @@ open class EventView: UIView {
5355
textView.textContainer.lineBreakMode = lineBreakMode
5456
}
5557
descriptor = event
56-
backgroundColor = event.backgroundColor
58+
backgroundColor = .clear
59+
layer.backgroundColor = event.backgroundColor.cgColor
60+
layer.cornerRadius = 5
5761
color = event.color
5862
eventResizeHandles.forEach{
5963
$0.borderColor = event.color
@@ -104,13 +108,16 @@ open class EventView: UIView {
104108
context.saveGState()
105109
context.setStrokeColor(color.cgColor)
106110
context.setLineWidth(3)
111+
context.setLineCap(.round)
107112
context.translateBy(x: 0, y: 0.5)
108113
let leftToRight = UIView.userInterfaceLayoutDirection(for: semanticContentAttribute) == .leftToRight
109114
let x: Double = leftToRight ? 0 : frame.width - 1.0 // 1 is the line width
110115
let y: Double = 0
116+
let hOffset: Double = 3
117+
let vOffset: Double = 5
111118
context.beginPath()
112-
context.move(to: CGPoint(x: x, y: y))
113-
context.addLine(to: CGPoint(x: x, y: (bounds).height))
119+
context.move(to: CGPoint(x: x + 2 * hOffset, y: y + vOffset))
120+
context.addLine(to: CGPoint(x: x + 2 * hOffset, y: (bounds).height - vOffset))
114121
context.strokePath()
115122
context.restoreGState()
116123
}
@@ -123,7 +130,7 @@ open class EventView: UIView {
123130
if UIView.userInterfaceLayoutDirection(for: semanticContentAttribute) == .rightToLeft {
124131
return CGRect(x: bounds.minX, y: bounds.minY, width: bounds.width - 3, height: bounds.height)
125132
} else {
126-
return CGRect(x: bounds.minX + 3, y: bounds.minY, width: bounds.width - 3, height: bounds.height)
133+
return CGRect(x: bounds.minX + 8, y: bounds.minY, width: bounds.width - 6, height: bounds.height)
127134
}
128135
}()
129136
if frame.minY < 0 {

0 commit comments

Comments
 (0)