Skip to content

Commit

Permalink
Merge pull request #1274 from wakmusic/1258-bug-fix-naming-input-cond…
Browse files Browse the repository at this point in the history
…ition

🔀 :: (#1258) 닉네임 입력 조건 추가
  • Loading branch information
KangTaeHoon authored Sep 1, 2024
2 parents 1329204 + a509a21 commit e7638e4
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 7 deletions.
8 changes: 6 additions & 2 deletions Projects/Features/BaseFeature/Resources/Base.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,17 @@
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="248" contentHorizontalAlignment="left" contentVerticalAlignment="center" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="cfO-nl-nfD">
<rect key="frame" x="20" y="145" width="316" height="18.666666666666657"/>
<rect key="frame" x="20" y="145" width="303" height="18.666666666666657"/>
<color key="textColor" name="gray800"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocorrectionType="no" spellCheckingType="no" returnKeyType="done" smartDashesType="no" smartInsertDeleteType="no" smartQuotesType="no"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="O6b-uG-uWT">
<rect key="frame" x="341" y="137.33333333333334" width="32" height="34"/>
<rect key="frame" x="328" y="142.33333333333334" width="45" height="24"/>
<constraints>
<constraint firstAttribute="height" constant="24" id="XQd-rR-LNT"/>
<constraint firstAttribute="width" constant="45" id="laI-2j-1tT"/>
</constraints>
<inset key="imageEdgeInsets" minX="0.0" minY="0.0" maxX="2.2250738585072014e-308" maxY="0.0"/>
<state key="normal" title="취소">
<color key="titleColor" name="gray400"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ private extension MultiPurposePopupViewController {
cancelButton.layer.borderWidth = 1
cancelButton.backgroundColor = .white
cancelButton.isHidden = true
cancelButton.contentEdgeInsets = .init(top: 3, left: 12, bottom: 3, right: 12)

confirmLabel.font = DesignSystemFontFamily.Pretendard.light.font(size: 12)
confirmLabel.isHidden = true
Expand Down Expand Up @@ -184,21 +183,43 @@ extension MultiPurposePopupViewController: UITextFieldDelegate {
replacementString string: String
) -> Bool {
guard let char = string.cString(using: String.Encoding.utf8) else { return false }
let isBackSpace = strcmp(char, "\\b")
let isBackSpace: Bool = strcmp(char, "\\b") == -92

let latinCharCount = textField.text?.alphabetCharacterCeilCount ?? 0
let currentText = textField.text ?? ""
let latinCharCount: Double = currentText.alphabetCharacterCount

if let lastChar = currentText.last,
latinCharCount <= Double(viewModel.type.textLimitCount) {
// 완성되지 않은 한글인 경우
if lastChar.isIncompleteHangul {
return true
}

// 완성된 한글이지만, 추가로 자음이 결합될 수 있는 경우
if !lastChar.isIncompleteHangul &&
lastChar.canAddAdditionalJongseong {
return true
}
}

guard isBackSpace || latinCharCount < Double(viewModel.type.textLimitCount) else { return false }

guard isBackSpace == -92 || latinCharCount < viewModel.type.textLimitCount else { return false }
return true
}
}

private extension String {
var alphabetCharacterCeilCount: Int {
var alphabetCharacterCount: Double {
let count = reduce(0) { count, char in
return count + (char.isAlphabetCharacter ? 0.5 : 1)
}
return count
}

var alphabetCharacterCeilCount: Int {
let count = reduce(0) { count, char in
return count + (char.isAlphabetCharacter ? 0.5 : 1)
}
return Int(ceil(count))
}
}
Expand All @@ -207,4 +228,60 @@ private extension Character {
var isAlphabetCharacter: Bool {
return self.unicodeScalars.allSatisfy { $0.isASCII && $0.properties.isAlphabetic }
}

/// 완성되지 않은 한글 여부
var isIncompleteHangul: Bool {
guard let scalar = unicodeScalars.first else { return false }

// 한글 범위에 있는지 확인 (유니코드 값 범위 체크)
let hangulBase: UInt32 = 0xAC00
let hangulEnd: UInt32 = 0xD7A3

if scalar.value >= hangulBase && scalar.value <= hangulEnd {
let syllableIndex = (scalar.value - hangulBase)
let isCompleted = syllableIndex % 28 != 0
return !isCompleted
}

// 완성되지 않은 자모나 조합 중인 경우
return (scalar.value >= 0x1100 && scalar.value <= 0x11FF) || // 초성 자모 (현대 한글에서 사용하는 초성, 중성, 종성 등의 조합용 자모)
(scalar.value >= 0x3130 && scalar.value <= 0x318F) || // 호환용 자모 (구성된 한글 자모, 옛 한글 자모 등)
(scalar.value >= 0xA960 && scalar.value <= 0xA97F) || // 확장 A (옛 한글 자모의 일부)
(scalar.value >= 0xD7B0 && scalar.value <= 0xD7FF) // 확장 B (옛 한글 자모의 일부)
}

/// 한글 음절이 종성을 가졌으나 추가적인 종성이 더 결합될 수 있는지 여부 확인
var canAddAdditionalJongseong: Bool {
guard let scalar = unicodeScalars.first else { return false }

// 한글 음절 유니코드 범위: U+AC00 ~ U+D7A3
let hangulBase: UInt32 = 0xAC00
let hangulEnd: UInt32 = 0xD7A3

guard scalar.value >= hangulBase && scalar.value <= hangulEnd else {
return false
}

// 종성에 해당하는 인덱스를 계산
let syllableIndex = scalar.value - hangulBase
let jongseongIndex = Int(syllableIndex % 28)

// 종성이 있을 때 추가 종성을 가질 수 있는 경우를 판별
let canHaveDoubleJongseong: Bool

switch jongseongIndex {
case 1: // ㄱ (U+11A8)
canHaveDoubleJongseong = true
case 4: // ㄴ (U+11AB)
canHaveDoubleJongseong = true
case 8: // ㄹ (U+11AF)
canHaveDoubleJongseong = true
case 17: // ㅂ (U+11B7)
canHaveDoubleJongseong = true
default:
canHaveDoubleJongseong = false
}

return canHaveDoubleJongseong
}
}

0 comments on commit e7638e4

Please sign in to comment.