Repository navigation
Expand file tree
/
Copy pathViewController.swift
More file actions
147 lines (120 loc) · 4.68 KB
/
Copy pathViewController.swift
File metadata and controls
147 lines (120 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//
// ViewController.swift
// WordCloudCollectionView
//
// Created by SonJihyun on 04/02/2018.
// Copyright © 2018 DoInsight. All rights reserved.
//
import UIKit
struct Topic: Codable {
let sentence: String
let frequency: UInt
}
class ViewController: UIViewController {
@IBOutlet weak var wordCloudCollectionView: UICollectionView!
var topicList = [Topic]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let topicString = """
[
{
"sentence": "끈적임 없는",
"frequency": 9
},
{
"sentence": "저자극의",
"frequency": 40
},
{
"sentence": "흡수력 좋은",
"frequency": 100
},
{
"sentence": "산뜻한",
"frequency": 20
},
{
"sentence": "유분감 적은",
"frequency": 50
},
{
"sentence": "약간 건조함",
"frequency": 5
},
{
"sentence": "피부장벽강화",
"frequency": 10
}
]
"""
do {
let data = topicString.data(using: .utf8)
let decoder = JSONDecoder()
let topicList = try decoder.decode([Topic].self, from: data!)
if topicList.count == 0 {
return
}
var shuffledTopicList = topicList
for index in shuffledTopicList.indices {
let toIndex: Int = numericCast(arc4random_uniform(numericCast(shuffledTopicList.count - 1)))
shuffledTopicList.swapAt(index, toIndex)
}
//제일 frequency 높은 것을 가운데로
let frequencyList = shuffledTopicList.map{$0.frequency}
let maxFrequency = frequencyList.max()
for i in 0..<frequencyList.count {
if shuffledTopicList[i].frequency == maxFrequency {
let maxTopic = shuffledTopicList[i]
shuffledTopicList.remove(at: i)
shuffledTopicList.insert(maxTopic, at: (shuffledTopicList.count + 1)/2)
}
}
if let flowLayout = self.wordCloudCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSize(width: 1, height:1)
flowLayout.minimumLineSpacing = 0
}
self.topicList = shuffledTopicList
self.wordCloudCollectionView.dataSource = self
self.wordCloudCollectionView.reloadData()
} catch {
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return topicList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "KeywordCollectionViewCell", for: indexPath)
if let sentenceLabel = cell.contentView.subviews.first as? UILabel {
sentenceLabel.text = topicList[indexPath.row].sentence
let frequencyList = topicList.map{$0.frequency}
let maxFrequency = frequencyList.max()
let minFrequency = frequencyList.min()
let maxFontSize:CGFloat = 36
let minFontSize:CGFloat = 12
let maxAlpha:CGFloat = 1
let minAlpha:CGFloat = 0.5
let fontSize = self.calculateItemValue(minValue: minFontSize, maxValue: maxFontSize, itemFrequency: topicList[indexPath.row].frequency, minFrequency: minFrequency!, maxFrequency: maxFrequency!)
sentenceLabel.font = sentenceLabel.font.withSize(fontSize)
sentenceLabel.alpha = self.calculateItemValue(minValue: minAlpha, maxValue: maxAlpha, itemFrequency: topicList[indexPath.row].frequency, minFrequency: minFrequency!, maxFrequency: maxFrequency!)
}
return cell
}
func calculateItemValue(minValue:CGFloat, maxValue:CGFloat, itemFrequency:UInt, minFrequency:UInt, maxFrequency:UInt) -> CGFloat {
if maxFrequency == minFrequency {
return (maxValue + minValue)/2
}
let itemFrequency = CGFloat(itemFrequency)
let minFrequency = CGFloat(minFrequency)
let maxFrequency = CGFloat(maxFrequency)
let a = (maxValue - minValue)/(maxFrequency-minFrequency)
let b = (maxFrequency*minValue-minFrequency*maxValue)/(maxFrequency - minFrequency)
return a*itemFrequency + b
}
}