Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/limit text #275

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ struct ContentRectangleView: View {
.opacity(currentPageIndex == 0 ? 1 : 0)

Text(letterContent.forceCharWrapping)
.lineSpacing(FontUtility.lineSpacing(font: fontString))
.kerning(FontUtility.kerning(font: fontString))
// .lineSpacing(FontUtility.lineSpacing(font: fontString))
// .kerning(FontUtility.kerning(font: fontString))
.foregroundStyle(.contentPrimary)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.top, screenHeight * 0.01)
Expand Down
49 changes: 19 additions & 30 deletions Kabinett/Presentation/View/WriteLetter/ContentWriteView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ struct CustomTextEditor: UIViewRepresentable {
var maxWidth: CGFloat
var maxHeight: CGFloat
var font: UIFont
// var lineSpacing: CGFloat
// var kerning: CGFloat

let maxCharacterLimit: Int = 397

class Coordinator: NSObject, UITextViewDelegate {
var parent: CustomTextEditor
Expand All @@ -336,11 +332,13 @@ struct CustomTextEditor: UIViewRepresentable {
}

func textViewDidChange(_ textView: UITextView) {
if textView.text.count > parent.maxCharacterLimit {
textView.text = String(textView.text.prefix(parent.maxCharacterLimit))
}
let size = textView.sizeThatFits(CGSize(width: parent.maxWidth, height: CGFloat.greatestFiniteMagnitude))

parent.text = textView.text
if size.height > parent.maxHeight {
textView.text = parent.text
} else {
parent.text = textView.text
}
}
}

Expand All @@ -359,35 +357,26 @@ struct CustomTextEditor: UIViewRepresentable {
textView.autocorrectionType = .no
textView.spellCheckingType = .no
textView.smartInsertDeleteType = .no
textView.font = font

let maxWidthConstraint = NSLayoutConstraint(item: textView,
attribute: .width,
relatedBy: .lessThanOrEqual,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: maxWidth)
let maxWidthConstraint = NSLayoutConstraint(
item: textView,
attribute: .width,
relatedBy: .lessThanOrEqual,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: maxWidth
)
textView.addConstraint(maxWidthConstraint)

return textView
}

func updateUIView(_ uiView: UITextView, context: Context) {
if uiView.text == text && uiView.font == font {
return
if uiView.text != text {
uiView.text = text
}

uiView.attributedText = createAttributedString(text: text, font: font)
}

private func createAttributedString(text: String, font: UIFont) -> NSAttributedString {
let paragraphStyle = NSMutableParagraphStyle()

let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.paragraphStyle: paragraphStyle
]

return NSAttributedString(string: text, attributes: attributes)
uiView.font = font
}
}
Loading