Changing the TextField direction according the keyboard language in SwiftUI - xcode

The default direction for the TextField is LTR. I want it to change to RTL automatically when the user changes the keyboard language to Arabic, and of course back to LTR when the keyboard language is changed to English.
This is my code in SwiftUI
TextField("Type here ...", text: $text)
.padding()
.background(Color.gray.opacity(0.1))
.cornerRadius(10)
Alos I want to change the text "Type here ..." to something in Arabic if possible.
Thanks

Related

Is there any way to control the width of the search field as it's being added to the toolbar while using .searchable on a macOS app?

I'm using SwiftUI to code a macOS app, and I'm adding a .searchable to it, placing the search field on the toolbar. macOS is adding it to the end of the toolbar, which is good. However, it's too wide.
The code is simple
.searchable(
text: $searchText,
placement: .toolbar,
prompt: "Search..."
)
Is there any way to control the width of the search field as it's being added to the toolbar?
Furthermore, is there a way to set it to always display as "minimized" the way some of the macOS apps do?
This is the way it looks on my app:
And this is what it looks like on one of the macOS apps: just a magnifying glass.
The search field resizes itself automatically and its appearance depends on the situation. If you have a resizable window and drag it smaller, you will end up in the appearance you describe.
Also if you add more elements or functions to the toolbar, the search field will eventually collapse to the magnifying glass only.
You could use this to add an empty but very wide Button field to the toolbar to "push" the search field to the side:
.toolbar {
ToolbarItemGroup {
Button("") {}
.frame(width: 400)
.buttonStyle(.plain)
}
}

SwiftUI TextField cursor with wrong size and position in MacOS

I am using a TextField in SwiftUI for a MacOS application. I am trying to change the font size of my TextField like this...
TextField("Name", text: $s_name)
.textFieldStyle(PlainTextFieldStyle())
.padding(5)
.font(.largeTitle)
.multilineTextAlignment(.center)
The increased font size does work, however the cursor of my TextField is completed off. I have disabled the focus ring of the TextField aswell, as stated in the answer of Asperi here
However the bug appears even without using this fix.
The problem appears when I press in the TextField for the first time. The cursor it totally off place and at the wrong size. If I start to type, it gets corrected.
Edit: Have reported this bug to Apple in their Feedback app.

SwiftUI selectable Text on macOS

How would one display text in swiftUI that can be selected? As in Terminal app on macOS for example?
Text("This is some text") displays text but it is not selectable.
I am new to GUI development on Mac. Should I be using some other View?
You can use it since macOS12 and iOS 15 as:
Text("Hello, world!")
.textSelection(.enabled)
Try to use Button to Wrap the Text or use the Button directly.
Button("This is some text") {
print("do something here")
}

What type of NSButton is it? In "About This Mac" window

What type of NSButton are those 6 buttons on right side of the screenshot?
Is it standard control? How can I implement such buttons?
They look like inline buttons but they are not.
That's an inline with image "NSFollowLinkFreeStandingTemplate", position "leading" and alignment "left", made in interface editor of xcode:

How do I change what is read by VoiceOver for an NSButton that has no Title (image only)?

I have a few NSButtons that are images only, and are set completely programmatically. When VoiceOver reads these buttons, it says "unchecked checkbox" for each of them. I'd like to set an attribute for VoiceOver to read that is more descriptive (for accessibility purposes), but I can't set the title attribute on the button because the title will then be displayed (I only want the icon to be displayed).
I tried using
[self.button accessibilitySetOverrideValue:title forAttribute:NSAccessibilityDescriptionAttribute];
but nothing changed. I also tried NSAccessibilityTitleAttribute, NSAccessibilityRoleDescriptionAttribute, and NSAccessibilityRoleAttribute with no luck. Am I doing something incorrectly?
Thanks.
It looks like the original code was correct, but it only works when called on the button cell (not the button itself). The above was changed to:
[self.button.cell accessibilitySetOverrideValue:title forAttribute:NSAccessibilityDescriptionAttribute];
where title is still an NSString*. Assuming title is #"Test", the above code causes VoiceOver to read "Test unchecked checkbox." The only way I found to have it not read the "unchecked checkbox" parts was to clear three other attributes: NSAccessibilityTitleAttribute, NSAccessibilityRoleDescriptionAttribute, and NSAccessibilityRoleAttribute. I overrode each of these with #" ". If anyone has a more elegant solution to ignore these fields, please let me know. For now, I'll mark this answered.
In Yosemite (10.10), you can use the method-based accessibility API to do this:
e.g., in a Custom NSView:
class CustomTabBarGroup: NSView, NSAccessibilityGroup {
required init?(coder: NSCoder) {
super.init(coder: coder)
// We function as a tab group — closest in role is a radio button group
self.setAccessibilityRole(NSAccessibilityRadioGroupRole)
// You can also override what the role normally reads:
self.setAccessibilityRoleDescription("Tab bar")
}
//...
}
And, maybe on a switch button that behaves like a radio button in your app (e.g., like a selected tab out of several):
class CustomTabBarButton: NSButton {
required init?(coder: NSCoder) {
super.init(coder:coder)
self.setAccessibilityRole(NSAccessibilityRadioButtonRole)
}
//...
}
For an example of controls with non-standard appearance adopting such roles, check out iPhoto’s Adjust panel. The right-hand tab bar with the "Quick fixes", "Effects", and "Adjust" tabs is actually a radio button group with radio buttons. Also the effect swatches under the Effects tab are steppers, checkboxes, and a button.

Resources