Post

Replies

Boosts

Views

Activity

sheet presentation with `sheet(item:onDismiss:content:)` and PropertyWrapper
When employing sheet(item:onDismiss:content:), are there specific considerations that we need to take into account? I have a class, Presenter, that centrally manages all presentations, and I utilize the DynamicProperty of CustomProperty to update UserDefaults (for simplicity, its code is omitted here). The provided code demonstrates the correct functionality of sheet(isPresented:onDismiss:content:). However, when incorporating sheet(item:) within the given structure, the slider fails to move, although the value is updated, and the reset button does not function. import SwiftUI @propertyWrapper private struct CustomProperty: DynamicProperty { @State private var value : CGFloat init(value: CGFloat) { self.value = value } var wrappedValue: CGFloat { get { value } nonmutating set { value = newValue } } var projectedValue: Binding<CGFloat> { .init(get: { wrappedValue }, set: { wrappedValue = $0 }) } } private struct SheetView: View { @Binding var fontSize: CGFloat var body: some View { VStack { Slider(value: $fontSize, in: 20...40) Button("reset") { fontSize = 30 } } } } @Observable private class Presenter { struct Sheet: Identifiable { let id = UUID() let view: AnyView } var currentSheet: Sheet? static let shared = Presenter() } struct SwiftUIView: View { @State var show: Bool = false @Bindable private var presenter = Presenter.shared @CustomProperty(value: 30) private var fontSize var body: some View { VStack { Text("fontSize: \(Int(fontSize))").bold() Button("show (isPresented:)") { show.toggle() } Button("show (item:)") { let sheet = Presenter.Sheet(view: AnyView(SheetView(fontSize: $fontSize))) Presenter.shared.currentSheet = sheet } } .sheet(isPresented: $show) { AnyView(SheetView(fontSize: $fontSize) .presentationDetents([.height(200)])) } .sheet(item: $presenter.currentSheet) { sheet in sheet.view .presentationDetents([.height(200)]) } } } #Preview { SwiftUIView() }
0
0
180
Jan ’24
UITextView iOS16 - access to layoutManager property of textview cause crash
The code has been working for serval years. now it crashes when running on iOS 16 and on the layoutManager = textView.layoutManager . It seems the TextKit 1 compatibility mode became active, but it still crashes on some of the textView if the first word is Arabic. static func didTapTerm(_ sender: UITapGestureRecognizer) -> String? {   guard let senderView = sender.view, (senderView is UITextView) else {     return nil   }   let textView = senderView as! UITextView,   layoutManager = textView.layoutManager       var location = sender.location(in: textView)   location.x -= textView.textContainerInset.left   location.y -= textView.textContainerInset.top       // find the value   // character index at tap location   let textContainer = textView.textContainer,   characterIndex = layoutManager.characterIndex(for: location, in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil),   textStorage = textView.textStorage       guard characterIndex < textStorage.length else {     return nil   }       var range = NSRange()   if let phrase = textStorage.attribute(customAttributedStringKey,                    at: characterIndex,                    effectiveRange: &range) {     if let ph = phrase as? String {       // update UI ...     }     return phrase as? String   }   return nil } This is a console log: 2022-09-15 13:29:55.532693-0700 DictionaryApp[1101:95240] [Text] UITextView 0x1051cd600 is switching to TextKit 1 compatibility mode because its layoutManager was accessed. Break on void _UITextViewEnablingCompatibilityMode(UITextView *__strong, BOOL) to debug. 2022-09-15 13:29:55.543047-0700 DictionaryApp[1101:95240] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1 2022-09-15 13:29:55.543166-0700 DictionaryApp[1101:95240] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange character count mismatch 2022-09-15 13:32:06.118905-0700 DictionaryApp[1101:95240] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range {5, 99} out of bounds; string length 103' *** First throw call stack: (0x1ab8f2248 0x1a4cbfa68 0x1a5d7c484 0x1b5b42260 0x1b5b5710c 0x1b5b55a14 0x1b5b73a20 0x1b5b73924 0x1b5b44df4 0x1b5b43cf4 0x1b5b639c0 0x1b5b61b08 0x1b5b61778 0x1b5b6125c 0x1ae9adcc4 0x1adaa76d0 0x1ae9ada3c 0x1adb8bfa0 0x1025ce384 0x102474d9c 0x102475154 0x1adb35164 0x1adea2dfc 0x1adc1cb94 0x1adafd1dc 0x1adba9358 0x1ae42a1d8 0x1adb6dd18 0x1adb72674 0x1adb71944 0x1adb71000 0x1adbb8d64 0x1adec14dc 0x1ab9be22c 0x1ab9ca614 0x1ab94e51c 0x1ab963eb8 0x1ab9691e4 0x1e4789368 0x1ade18d88 0x1ade189ec 0x102661fb0 0x1c9c8d948) libc++abi: terminating with uncaught exception of type NSException *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSBigMutableString _getBlockStart:end:contentsEnd:forRange:stopAtLineSeparators:]: Range {5, 99} out of bounds; string length 103' terminating with uncaught exception of type NSException This is the backtrace:
0
3
1.6k
Sep ’22