Instead of implementing a textview from scratch (UITextInput it a lot of work/boilerplate)
It makes sense for me to subclass UITextView.
However, when subclassing it seems this is limited to TextKit 1 only, I get an assertion failure:
*** Assertion failure in -[_UITextKit1LayoutController initWithTextView:textContainer:], _UITextKit1LayoutController.m:72
I thought I would just need to call the super init:
super.init(usingTextLayoutManager: true)
But this isn't a designated initialiser:
Must call a designated initializer of the superclass 'UITextView'
Is there a way to do this and override the layout manager so that it uses TextKit 2 in the subclass?
(My aim is to then draw the fragments manually using TextKit2 to get a custom layout while ultimately using all of the UITextView implementation as 99% of it is what I want - other than custom drawing of text fragments).
My code is below:
class DocumentTextView: UITextView {
private let _textLayoutManager = NSTextLayoutManager()
private var textContentStorage: NSTextContentStorage {
textLayoutManager!.textContentManager as! NSTextContentStorage
}
override var textLayoutManager: NSTextLayoutManager? {
_textLayoutManager
}
init() {
let textContainer = NSTextContainer(size: .zero)
super.init(frame: .zero, textContainer: textContainer)
_textLayoutManager.textContainer = textContainer
textContentStorage.attributedString = NSAttributedString(string: text, attributes: [
.foregroundColor: UIColor.label,
])
textContentStorage.addTextLayoutManager(_textLayoutManager)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Post
Replies
Boosts
Views
Activity
I'm trying to unpack this sentence
Note that after adding a second text container to the layout manager, the text views become uneditable and unselectable.
Found on this example code:
https://developer.apple.com/documentation/uikit/textkit/display_text_with_a_custom_layout
I'm implementing my own UITextInput which makes use of the example code setup to replicate a multi-page text editor (kind of like a basic version of Pages/Word)
Each page has its own NSTextContainer which gets added to one NSLayoutManager.
The code works and my views are rendering text across each page, however, only the first text view is editable.
It appears this is "by design" (?)
How do we get around this? Is there another level we have to drop down to enable multiple editing containers?
The sample code is wayback in iOS 13, so it would seem very odd if the capability of multiple editable text fields/containers was impossible to achieve.
Hellooo,
I’m looking to implement an OpenAI assistant using APIs but I want to do this locally on a group of files.
I want to be able to train a GPT on the contents of a folder for example.
Does anyone have any experience in this?
It seems OpenAI needs a lot of uploading on each request if I were to do this with their API after playing around (but this feels like I’m missing something). It’s also quite costly to use.
I was hoping to use local machine learning and models but this is quite limited in what it can do (eg Lumachain)
Originally asked on Swift Forums: https://forums.swift.org/t/using-bindable-with-a-observable-type/70993
I'm using SwiftUI environments in my app to hold a preferences object which is an @Observable object
But I want to be able to inject different instances of the preferences object for previews vs the production code so I've abstracted my production object in to a Preferences protocol and updated my Environment key's type to:
protocol Preferences { }
@Observable
final class MyPreferencesObject: Preferences { }
@Observable
final class MyPreviewsObject: Preferences { }
// Environment key
private struct PreferencesKey: EnvironmentKey {
static let defaultValue : Preferences & Observable = MyPreferencesObject()
}
extension EnvironmentValues {
var preferences: Preferences & Observable {
get { self[PreferencesKey.self] }
set { self[PreferencesKey.self] = newValue }
}
}
The compiler is happy with this until I go to use @Bindable in my code where the compiler explodes with a generic error,
eg:
@Environment(\.preferences) private var preferences
// ... code
@Bindable var preferences = preferences
If I change the environment object back to a conforming type eg:
@Observable
final class MyPreferencesObject() { }
private struct PreferencesKey: EnvironmentKey {
static let defaultValue : MyPreferencesObject = MyPreferencesObject()
}
extension EnvironmentValues {
var preferences: MyPreferencesObject {
get { self[PreferencesKey.self] }
set { self[PreferencesKey.self] = newValue }
}
}
Then @Bindable is happy again and things compile.
Is this a known issue/limitation? Or am I missing something here?
When running a SwiftUI Multiplatform app in Xcode Cloud I'm getting the following error:
CIWatcher encountered an error in CIWatcherTests failed with:
Failed to install or launch the test runner. (Underlying Error: Could not launch “CIWatcherTests”. Runningboard has returned error 5. Please check the system logs for the underlying cause of the error. (Underlying Error: The operation couldn’t be completed. Launch failed. (Underlying Error: Launchd job spawn failed)))
This runs fine on iOS and all those tests pass.
The SwiftUI app on Mac is a menu bar app so I'm not sure if this could be the issue as it's not launching for some reason on the Xcode Cloud system? Maybe I need to add some run option to make this work?
When run locally both iOS and macOS pass all the tests successfully
Sometimes we just want to use modifiers but don't have a view to draw to the screen, for example an .overlay {} modifier on a view needs a view inside it before you can add modifiers on to that overlay view.
The current approach for this type of thing seems to be Color.clear and while I've seen this almost everywhere I'm not sure if this is
a) the officially recommended way to do this in SwiftUI and
b) the only way to do this in SwiftUI?
I have the following custom view in my project just to avoid the explicit use of Color.clear
import SwiftUI
struct ClearView: View {
var body: some View {
Color.clear
}
}
Are there any other ways to do this?
Note - this is of course not the same as EmptyView() - where Color.clear renders a view with a transparent colour and EmptyView which doesn't render anything at all.
When: Embedding a Image with a symbol effect in a stack view in SwiftUI, and putting this in a List
Actual: The symbols animate when scrolling that item in and out of the view
Expected: The symbol doesn't animate until the value changes.
I have the following code which is boiled down from my actual code where the animating value is a @Model object property (other animations work fine when their animation's value is watching the same property.)
I've boiled it down to the below examples:
The top example works as expected (no symbol animations on scroll)
The bottom example animates the symbols every time they scroll in to view
#Preview {
VStack {
List {
ForEach(1...100, id: \.self) { index in
Image(systemName: "square.3.layers.3d")
.symbolEffect(.bounce, value: index)
}
}
List {
ForEach(1...100, id: \.self) { index in
HStack {
Image(systemName: "square.3.layers.3d")
.symbolEffect(.bounce, value: index)
}
}
}
}
}
This happens on Mac and iOS.
I'm running Xcode 15.3 RC2 but it also happens in Xcode 15.2
I've found this will happen whether embedding in a HStack, VStack or ZStack but seems fine in a Group.
I've tried adding a id() modifier to the HStack to give it an explicit identifier but this doesn't seem to work.
Is there something I'm missing here?
Throughout the various way across the internet of capturing a screenshot of the entirety of a UIScrollView's contents (not just the clipped viewport), all pieces of code seem to fail and draw the correct size of image but still be clipped to the viewport of the UIScrollView only.
I can't seem to see any official say on this and if it is a bug has existed at least until iOS 13 (and is still in iOS 15)
Does anyone know if this is a known issue (Apple Engineers)?
Most code examples look like this https://stackoverflow.com/a/48590443/1735584
but there are also examples with drawInHierarchy and snapshotView which results in the same thing
I'm using the UICollectionView drag and drop API to drag cells between collection views as well as within the same collection view.This works as expected when dragging a cell within its original collection view, however, when I drag this cell over another collection view I get the following:... *** Assertion failure in -[UICollectionView _updateRowsAtIndexPaths:updateAction:updates:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3901.4.2/UICollectionView.m:6265Event with its simplest implementation for dropSessionDidUpdate: this happpens:func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal { return .init(operation: .move, intent: .insertAtDestinationIndexPath) }This always happens when the operation is .move or .copy.to my understanding this should "just work" and dropSessionDidUpdate: should provide nice visual feedback about the potential drop here.It's worth also noting the collection views all use the same code (each a UICollectionViewController of the same type).Update 1This seems to "work" when returning .init(operation: .move) without the intent parameter. Although there are no animations, the data is moved and the collection view updated...Update 2As above, this also works when returning .init(operation: .move, intent: .insertIntoDestinationIndexPath) or .init(operation: .move, intent: .unspecified). There are no suggestive animations but the content is dropped successfully.
I'm getting the following log to the console when trying to use diffable data source and uicollection view drag and drop. I can drag cells within the same collection view, but when a cell comes near a "different" collection view (they're the same class but different instances so should handle the drop) I get the following:2020-01-21 22:20:44.685944+0000 MyApp[1888:1802040] *** Assertion failure in -[UICollectionView _updateRowsAtIndexPaths:updateAction:updates:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3901.4.2/UICollectionView.m:62652020-01-21 22:20:50.466589+0000 MyApp[1888:1802249] XPC connection interrupted2020-01-21 22:20:50.469098+0000 MyApp[1888:1802040] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView must be updated via the UICollectionViewDiffableDataSource APIs when acting as the UICollectionView's dataSource: please do not call mutation APIs directly on UICollectionView. <UICollectionView: 0x7f950f022400; frame = (0 0; 1194 834); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x60000231dcb0>; layer = <CALayer: 0x600002deea00>; contentOffset: {-8, 0}; contentSize: {1344, 834}; adjustedContentInset: {0, 8, 0, 8}; layout: <UICollectionViewCompositionalLayout: 0x7f950e719990>; dataSource: <_TtGC5UIKit34UICollectionViewDiffableDataSourceOC9MyApp30DocumentViewControllerP10$104db5acc7SectionOS2_P10$104db5b5c4Item_: 0x600002fa97c0>>'*** First throw call stack:( 0 CoreFoundation 0x00007fff23c7127e __exceptionPreprocess + 350 1 libobjc.A.dylib 0x00007fff513fbb20 objc_exception_throw + 48 2 CoreFoundation 0x00007fff23c70ff8 +[NSException raise:format:arguments:] + 88 3 Foundation 0x00007fff256e9b51 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191 4 UIKitCore 0x00007fff478823f9 -[UICollectionView _updateRowsAtIndexPaths:updateAction:updates:] + 720 5 UIKitCore 0x00007fff478dbf02 __84-[_UICollectionViewDragAndDropController _beginDragAndDropInsertingItemAtIndexPath:]_block_invoke + 124 6 UIKitCore 0x00007fff4789e6f2 -[UICollectionView _performShadowUpdates:] + 141 7 UIKitCore 0x00007fff478dbb3f -[_UICollectionViewDragAndDropController _beginDragAndDropInsertingItemAtIndexPath:] + 180 8 UIKitCore 0x00007fff478da20d -[_UICollectionViewDragAndDropController beginReorderingForItemAtIndexPath:cell:] + 281 9 UIKitCore 0x00007fff478994a7 -[UICollectionView _beginInteractiveMovementForItemAtIndexPath:] + 309 10 UIKitCore 0x00007fff478e7ac5 -[_UICollectionViewDragDestinationController _reorderingDisplayLinkDidTick] + 1651 11 QuartzCore 0x00007fff2afeb266 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 640 12 QuartzCore 0x00007fff2b0c3e03 _ZL22display_timer_callbackP12__CFMachPortPvlS1_ + 299 13 CoreFoundation 0x00007fff23b9503d __CFMachPortPerform + 157 14 CoreFoundation 0x00007fff23bd4bc9 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41 15 CoreFoundation 0x00007fff23bd4228 __CFRunLoopDoSource1 + 472 16 CoreFoundation 0x00007fff23bced64 __CFRunLoopRun + 2516 17 CoreFoundation 0x00007fff23bce066 CFRunLoopRunSpecific + 438 18 GraphicsServices 0x00007fff384c0bb0 GSEventRunModal + 65 19 UIKitCore 0x00007fff48092d4d UIApplicationMain + 1621 20 MyApp 0x0000000104d92deb main + 75 21 libdyld.dylib 0x00007fff5227ec25 start + 1 22 ??? 0x0000000000000001 0x0 + 1)libc++abi.dylib: terminating with uncaught exception of type NSException