# 일기장 [STEP 1] # STEP 1 PR 안녕하세요 지성(@yim2627)! 일기장 STEP 1 PR 보내드립니다. 설레이는 일기장 첫 번째 PR입니다. 🙌 첫 번째 리뷰 잘 부탁드립니다!🙏 ## 고민했던 점 1️⃣ **StackView 내부에서 Label의 Height가 잡히지 않는 현상.** StackView 내부에서 Label의 Height가 잡히지 않는 현상이 있었습니다.</br>height is ambiguous for UILabel 경고가 생겨 고민이 많았습니다.</br> **🙋‍♂️ diaryTitle과 dateAndPreview의 content hugging priority 가 같아 생기는 현상이였습니다.</br>따라서 diaryTitle에 .defaultHigh + 1 값을 주었고, dateAndPreview에는 .defaultHigh값을 주어 각각 다른 content hugging priority값을 주어 해결하였습니다.** ```swift! import UIKit class DiaryTableViewCell: UITableViewCell { private let diaryTitle: UILabel = { let label = UILabel() label.font = UIFont.preferredFont(forTextStyle: .title2) label.setContentHuggingPriority(.defaultHigh + 1, for: .vertical) return label }() private let dateAndPreview: UILabel = { let label = UILabel() label.font = UIFont.preferredFont(forTextStyle: .body) label.setContentHuggingPriority(.defaultHigh, for: .vertical) return label }() } ``` 2️⃣ **NSAttributedString이 적용되지 않는 현상.** [__NSCFConstantString renderingMode]: unrecognized selector sent to instance 0x10aa5a2e8 에러가 발생했습니다.</br>고민을 하다가 원인을 찾았습니다.</br>원인은 NSAttributedString.Key.font에 필요한 타입이 UIFont였으나 UIFont.TextStyle을 사용 중이었습니다.</br> 🙋‍♂️**NSAttributedString.Key.font에 UIFont 타입으로 전달하여 해결했습니다.**</br> ```swift private func convertAttributedString(text: String, font: UIFont) -> NSAttributedString { let attributes = [NSAttributedString.Key.font: font as Any] as [NSAttributedString.Key : Any] let attributedString = NSAttributedString(string: text, attributes: attributes) return attributedString } ``` 3️⃣ **NSAttributedString의 가운데 정렬** 문자열 자체의 모양이 아래 이미지와 같기 때문에 UILabel의 baselineAdjustment로 가운데 정렬을 맞출 수 없었습니다.</br> <img src = "https://github.com/devKobe24/images/blob/main/NSAttributedString%E1%84%86%E1%85%AE%E1%86%AB%E1%84%8C%E1%85%A6%E1%84%8C%E1%85%A5%E1%86%B7.png?raw=true"></br> 그에 따라 다음과 같은 해결책을 찾았습니다.</br> **🙋‍♂️NSAttributedString.Key.baselineOffset을 이용해 작은 문자 부분의 baseline을 올리는 것으로 해결했습니다.**</br> <img src = "https://github.com/devKobe24/images/blob/main/NSAttributedString%E1%84%92%E1%85%A2%E1%84%80%E1%85%A7%E1%86%AF%E1%84%8C%E1%85%A5%E1%86%B7.png?raw=true"></br> ```swift private func attributedDateAndPreview(data: Diary, font: UIFont) -> NSMutableAttributedString { let text = "\(formatCreatedAt(data.createdAt)) \(data.body)" let attributedString = NSMutableAttributedString(string: text) let attributes: [NSAttributedString.Key: Any] = [ .font: font, .baselineOffset: 2 ] attributedString.addAttributes(attributes, range: (text as NSString).range(of: data.body)) return attributedString } ``` ## 조언을 얻고 싶은 부분 🙋‍♂️ 각 나라의 문자마다 크기와 길이가 다른데 어떻게 문자의 정렬을 맞추어 줄 수 있을까요? 🙋‍♂️ 저희가 Model 중 `DiaryDateFormatter`를 `싱글톤 패턴`으로 만들었습니다. `DateFormatter`의 인스턴스를 하나만 사용할 수 있는 다른 방법이 있을까요? 🙋‍♂️ POP 잘하는 방법, 꿀팁 알려주세요 ㅠㅠ 🙋‍♂️ Protocol명을 `IdentifierGenerator`로 명명했는데 괜찮은지 궁금합니다. ---