Skip to content

Commit

Permalink
#3 Edit writeView ScrollView -> UICollectionView
Browse files Browse the repository at this point in the history
  • Loading branch information
JunseokNoh committed Aug 3, 2021
1 parent 72da9d1 commit 8517e46
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 89 deletions.
4 changes: 4 additions & 0 deletions KNU_CSE/KNU_CSE.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
681D382026ADAE85000D9FE8 /* ReplyViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 681D381F26ADAE85000D9FE8 /* ReplyViewModel.swift */; };
681D382326ADC5D4000D9FE8 /* BoardSearchView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 681D382226ADC5D4000D9FE8 /* BoardSearchView.swift */; };
681D382626ADC61D000D9FE8 /* BoardWriteView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 681D382526ADC61D000D9FE8 /* BoardWriteView.swift */; };
68C7629126B96EFA00792498 /* CategoryCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68C7629026B96EFA00792498 /* CategoryCell.swift */; };
68F1A5B826A0088B007B4B86 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68F1A5B726A0088B007B4B86 /* AppDelegate.swift */; };
68F1A5BA26A0088B007B4B86 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68F1A5B926A0088B007B4B86 /* SceneDelegate.swift */; };
68F1A5BC26A0088B007B4B86 /* SignInView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68F1A5BB26A0088B007B4B86 /* SignInView.swift */; };
Expand Down Expand Up @@ -157,6 +158,7 @@
681D381F26ADAE85000D9FE8 /* ReplyViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyViewModel.swift; sourceTree = "<group>"; };
681D382226ADC5D4000D9FE8 /* BoardSearchView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoardSearchView.swift; sourceTree = "<group>"; };
681D382526ADC61D000D9FE8 /* BoardWriteView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoardWriteView.swift; sourceTree = "<group>"; };
68C7629026B96EFA00792498 /* CategoryCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CategoryCell.swift; sourceTree = "<group>"; };
68F1A5B426A0088B007B4B86 /* KNU_CSE.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KNU_CSE.app; sourceTree = BUILT_PRODUCTS_DIR; };
68F1A5B726A0088B007B4B86 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
68F1A5B926A0088B007B4B86 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -291,6 +293,7 @@
681D381026AAA908000D9FE8 /* ReplyCell.swift */,
68F861C426B1A6CF00934BD2 /* MyPageViewCell.swift */,
68F861DE26B2D31D00934BD2 /* ProfileTableCell.swift */,
68C7629026B96EFA00792498 /* CategoryCell.swift */,
);
path = Cell;
sourceTree = "<group>";
Expand Down Expand Up @@ -961,6 +964,7 @@
681D37F926A73F71000D9FE8 /* BoardTitleCollectionCell.swift in Sources */,
68F861DF26B2D31D00934BD2 /* ProfileTableCell.swift in Sources */,
68F1A5FE26A07147007B4B86 /* SignUpUILabel.swift in Sources */,
68C7629126B96EFA00792498 /* CategoryCell.swift in Sources */,
681D37DD26A5BF2D000D9FE8 /* ClassSeatCell.swift in Sources */,
681D37CF26A48A4B000D9FE8 /* FreeBoardViewModel.swift in Sources */,
681D37F226A70FE2000D9FE8 /* ReservationCheckDelegate.swift in Sources */,
Expand Down
80 changes: 80 additions & 0 deletions KNU_CSE/KNU_CSE/Custom/Cell/CategoryCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// CategoryCell.swift
// KNU_CSE
//
// Created by junseok on 2021/08/03.
//

import UIKit

class CategoryCell : UICollectionViewCell {
var titleLabel = UILabel()
static let identifier = "CategoryCell"

static func fittingSize(name: String) -> CGSize {
let cell = CategoryCell()
cell.setTitle(title: name)

let cellheight = cell.titleLabel.font.lineHeight + 10
let targetSize = CGSize(width: UIView.layoutFittingCompressedSize.width, height: cellheight)
return cell.contentView.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: .fittingSizeLevel, verticalFittingPriority: .required)
}

override var isSelected: Bool {
willSet {
if newValue {
self.titleLabel.textColor = .white
self.contentView.backgroundColor = Color.mainColor.withAlphaComponent(0.65)
} else {
self.titleLabel.textColor = .lightGray
self.contentView.backgroundColor = .white
}
}
}

override func prepareForReuse() {
//isSelected = false
}

override init(frame: CGRect) {
super.init(frame: frame)
draw()
self.isSelected = false
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func draw(){
initUI()
addView()
setUpConstraints()
}

func initUI(){
self.contentView.layer.borderWidth = 0.3
self.contentView.layer.cornerRadius = 10
self.contentView.layer.borderColor = UIColor.lightGray.cgColor

}

func addView(){
self.contentView.addSubview(titleLabel)
}

func setUpConstraints(){
titleLabel.snp.makeConstraints{ make in
make.left.right.equalToSuperview().inset(10)
make.top.bottom.equalToSuperview().inset(4)
}
}
}

extension CategoryCell{
func setTitle(title: String) {
titleLabel.text = title
titleLabel.textAlignment = .center
titleLabel.font = UIFont.systemFont(ofSize: 15, weight: .semibold)
}
}
1 change: 1 addition & 0 deletions KNU_CSE/KNU_CSE/Custom/Cell/CommentCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class CommentCell : UIView {
}
}


var authorLabel:UILabel!{
didSet{
authorLabel.text = comment.author
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,6 @@ class BoardDetailView:UIViewController, ViewProtocol, BoardDataDelegate{
self.authorLabel.snp.makeConstraints{ make in
make.centerY.equalTo(self.authorImageView)
make.left.equalTo(self.authorImageView.snp.right).offset(5)
//make.width.equalToSuperview().multipliedBy(0.4)
make.height.equalTo(height*0.1)
}

self.dateLabel.snp.makeConstraints{ make in
Expand Down
156 changes: 69 additions & 87 deletions KNU_CSE/KNU_CSE/Main Tab/Board/BoardWrite/BoardWriteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,95 +21,96 @@ class BoardWriteView:UIViewController, ViewProtocol{
var textFieldHeight:CGFloat!
var titleField:UITextField!{
didSet{
titleField.placeholder = "제목을 입력하세요."
titleField.delegate = self
titleField.font = UIFont.systemFont(ofSize: 17, weight: .regular)
titleField.addTarget(self, action: #selector(setRightItemColor), for: .editingChanged)
if let font = titleField.font {
self.titleField.placeholder = "제목을 입력하세요."
self.titleField.delegate = self
self.titleField.font = UIFont.systemFont(ofSize: 17, weight: .regular)
self.titleField.addTarget(self, action: #selector(setRightItemColor), for: .editingChanged)
if let font = self.titleField.font {
self.textFieldHeight = font.lineHeight + 20
}
}
}

var borderLine:UIView!{
didSet{
borderLine.layer.borderWidth = 0.3
borderLine.layer.borderColor = UIColor.lightGray.cgColor
self.borderLine.layer.borderWidth = 0.3
self.borderLine.layer.borderColor = UIColor.lightGray.cgColor
}
}

var categoryLabel:UILabel!{
didSet{
categoryLabel.text = "추천 카테고리"
self.categoryLabel.text = "추천 카테고리"
}
}

var categoryScroll:UIScrollView!{
var categoryTable:UICollectionView!{
didSet{
categoryScroll.alwaysBounceHorizontal = true
}
}

var categoryStack:UIStackView!{
didSet{
categoryStack.axis = .horizontal
categoryStack.distribution = .equalSpacing
categoryStack.spacing = 10
self.addStackView()
let flowLayout = UICollectionViewFlowLayout()
flowLayout.minimumLineSpacing = .zero
flowLayout.minimumInteritemSpacing = 16
flowLayout.scrollDirection = .horizontal
flowLayout.sectionInset = .init(top: 5, left: 5, bottom: 5, right: 5)

self.categoryTable.setCollectionViewLayout(flowLayout, animated: false)
self.categoryTable.delegate = self
self.categoryTable.dataSource = self
self.categoryTable.backgroundColor = .white
self.categoryTable.register(CategoryCell.self, forCellWithReuseIdentifier: CategoryCell.identifier)
}
}

var contentField:UITextView!{
didSet{
contentField.delegate = self
contentField.font = UIFont.systemFont(ofSize: 17, weight: .regular)
contentField.isScrollEnabled = false
self.contentField.delegate = self
self.contentField.font = UIFont.systemFont(ofSize: 17, weight: .regular)
self.contentField.isScrollEnabled = false
}
}

let placeHolderText = "내용을 입력하세요."
var contentPlaceHolder:UILabel!{
didSet{
contentPlaceHolder.text = placeHolderText
contentPlaceHolder.font = contentField.font
contentPlaceHolder.textColor = UIColor.lightGray.withAlphaComponent(0.75)
contentPlaceHolder.isHidden = !contentField.text.isEmpty
self.contentPlaceHolder.text = self.placeHolderText
self.contentPlaceHolder.font = self.contentField.font
self.contentPlaceHolder.textColor = UIColor.lightGray.withAlphaComponent(0.75)
self.contentPlaceHolder.isHidden = !self.contentField.text.isEmpty
}
}

var contentCheck:Bool = false
var rightItemButton:UIBarButtonItem!{
didSet{
rightItemButton.title = "작성"
rightItemButton.style = .plain
rightItemButton.tintColor = .white.withAlphaComponent(0.7)
rightItemButton.target = self
rightItemButton.action = #selector(addTapped)
self.navigationItem.rightBarButtonItem = rightItemButton
self.rightItemButton.title = "작성"
self.rightItemButton.style = .plain
self.rightItemButton.tintColor = .white.withAlphaComponent(0.7)
self.rightItemButton.target = self
self.rightItemButton.action = #selector(addTapped)
self.navigationItem.rightBarButtonItem = self.rightItemButton
}
}

let menu:[String] = ["잡담하기", "정보구하기", "팀원 구하기"]
var navigatiopDropDown:BTNavigationDropdownMenu!{
didSet{
navigatiopDropDown.menuTitleColor = .white
self.navigatiopDropDown.menuTitleColor = .white

//cell detail setting
navigatiopDropDown.cellHeight = 40
navigatiopDropDown.cellBackgroundColor = Color.mainColor
navigatiopDropDown.cellSelectionColor = Color.mainColor.withAlphaComponent(0.5)
navigatiopDropDown.cellTextLabelFont = UIFont.systemFont(ofSize: 17, weight: .semibold)
navigatiopDropDown.cellTextLabelColor = UIColor.white
navigatiopDropDown.cellTextLabelAlignment = .center
navigatiopDropDown.cellSeparatorColor = .white
self.navigatiopDropDown.cellHeight = 40
self.navigatiopDropDown.cellBackgroundColor = Color.mainColor
self.navigatiopDropDown.cellSelectionColor = Color.mainColor.withAlphaComponent(0.5)
self.navigatiopDropDown.cellTextLabelFont = UIFont.systemFont(ofSize: 17, weight: .semibold)
self.navigatiopDropDown.cellTextLabelColor = UIColor.white
self.navigatiopDropDown.cellTextLabelAlignment = .center
self.navigatiopDropDown.cellSeparatorColor = .white

navigatiopDropDown.animationDuration = 0.5
self.navigatiopDropDown.animationDuration = 0.5

//if dropdown show then setup backgroundcolor
navigatiopDropDown.maskBackgroundOpacity = 0.1
navigatiopDropDown.maskBackgroundColor = .black
self.navigatiopDropDown.maskBackgroundOpacity = 0.1
self.navigatiopDropDown.maskBackgroundColor = .black

self.navigationItem.titleView = navigatiopDropDown
self.navigationItem.titleView = self.navigatiopDropDown
self.navigatiopDropDown.didSelectItemAtIndexHandler = {[weak self] (indexPath: Int) -> () in
print("Did select item at index: \(indexPath)")
}
Expand All @@ -135,8 +136,7 @@ class BoardWriteView:UIViewController, ViewProtocol{
self.borderLine = UIView()

self.categoryLabel = UILabel()
self.categoryScroll = UIScrollView()
self.categoryStack = UIStackView()
self.categoryTable = UICollectionView(frame: CGRect(), collectionViewLayout: UICollectionViewFlowLayout())

self.contentField = UITextView()
self.contentPlaceHolder = UILabel()
Expand All @@ -146,17 +146,16 @@ class BoardWriteView:UIViewController, ViewProtocol{

func addView() {
self.view.addSubview(scrollView)
_ = [titleField, borderLine, contentField, contentPlaceHolder, categoryLabel, categoryScroll].map{
_ = [self.titleField, self.borderLine, self.contentField, self.contentPlaceHolder, self.categoryLabel, self.categoryTable].map{
self.scrollView.addSubview($0)
}
self.categoryScroll.addSubview(categoryStack)
}

func setUpConstraints() {
let left_margin = 20
let right_margin = -20
let padding = 5
let category_height = 50
let category_height = 55

self.scrollView.snp.makeConstraints{ make in
make.top.equalTo(self.view.safeAreaLayoutGuide)
Expand All @@ -179,19 +178,15 @@ class BoardWriteView:UIViewController, ViewProtocol{
make.height.equalTo(0.3)
}

self.categoryScroll.snp.makeConstraints{ make in
make.top.equalTo(self.borderLine.snp.bottom).offset(10)
self.categoryTable.snp.makeConstraints{ make in
make.top.equalTo(self.borderLine.snp.bottom).offset(5)
make.left.equalTo(self.view.safeAreaLayoutGuide).offset(left_margin)
make.right.equalTo(self.view.safeAreaLayoutGuide).offset(right_margin)
make.height.equalTo(category_height)
}

self.categoryStack.snp.makeConstraints{ make in
make.left.right.top.bottom.equalToSuperview()
}

self.contentField.snp.makeConstraints{ make in
make.top.equalTo(self.categoryScroll.snp.bottom).offset(10)
make.top.equalTo(self.categoryTable.snp.bottom).offset(10)
make.left.equalTo(self.view.safeAreaLayoutGuide).offset(left_margin)
make.right.equalTo(self.view.safeAreaLayoutGuide).offset(right_margin)
make.bottom.equalToSuperview()
Expand Down Expand Up @@ -267,40 +262,27 @@ extension BoardWriteView:UITextFieldDelegate{
}
}


extension BoardWriteView{
func addStackView(){
for i in 0..<categoryTitle.count{
self.categoryStack.addArrangedSubview(addingCustomButton(buttonTitle: categoryTitle[i], buttonFontSize: 15, buttonCount: 1))
}
extension BoardWriteView:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.categoryTitle.count
}

func addingCustomButton(buttonTitle : String, buttonFontSize: CGFloat, buttonCount : Int) -> UIButton
{
let ownButton = UIButton()
ownButton.setTitle(buttonTitle, for: UIControl.State.normal)
ownButton.titleLabel?.font = UIFont.systemFont(ofSize: buttonFontSize)
ownButton.backgroundColor = .white
ownButton.setTitleColor(UIColor.black, for: .normal)
ownButton.setTitleColor(UIColor.black.withAlphaComponent(0.5), for: .highlighted)
ownButton.addTarget(self, action: #selector(ownButtonAction), for: .touchUpInside)
ownButton.layer.borderWidth = 0.5
ownButton.layer.borderColor = UIColor.lightGray.cgColor
ownButton.layer.cornerRadius = 10

let buttonTitleSize = (buttonTitle as NSString).size(withAttributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: buttonFontSize + 1)])

let height = buttonTitleSize.height * 1.5
let width = buttonTitleSize.width + 20
ownButton.snp.makeConstraints{ make in
make.width.equalTo(width)
make.height.equalTo(height)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CategoryCell.identifier, for: indexPath) as? CategoryCell else{
return UICollectionViewCell()
}
return ownButton
cell.setTitle(title: self.categoryTitle[indexPath.row])

return cell
}

@objc func ownButtonAction(sender: UIButton)
{
print("\n\n Title \(sender.titleLabel?.text)")

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CategoryCell.fittingSize(name: categoryTitle[indexPath.row])
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == categoryTable {
print(categoryTitle[indexPath.row])
}
}
}

0 comments on commit 8517e46

Please sign in to comment.