I have UIAlertController with a message. I want the alert message accessibility label to be different than the alert message string.
How can I do that?
you can find each UIControl element using subviews array.
For example if you want to change accessibilityLabel of your UIAlertController you can do this:
let contentTitleForAlert = myAlertController.view.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0].subviews[0]
contentTitleForAlert.accessibilityLabel = "¡My text for accessibility label"
Using this way you can change other elements in the UIAlertController. For example:
let contentLabelForAlert = myalertController.view.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0].subviews[1]
contentLabelForAlert.accessibilityLabel = "Text for accessibilityLabel for main content in UIAlertController."
Related
I am setting UIAlertAction label attributedText in order to set custom Font in UIAlertController.It works but when i tap on the UIAlertAction it changes its font to default for some time and then disappears. Here is the code
let lb = (action.value(forKey: "__representer") as AnyObject)
let label = lb.value(forKey: "label") as? UILabel
label?.attributedText = myMutableString
I think best idea would be to create your own custom alert controller instead of accessing private properties to set your values. Not sure if Apple would accept it or not.
I don't know the solution for your problem but I know the problem which is
Problem: When you press UIAlertAction's button it changes its state. It behaves just like the states of UIButton. So if you know which property you should set for its highlighted state then you can set that to solve this issue.
How do you type into a UITextView inside a XCTestCase? The UITextView is the first responder so it already had focus and the keyboard is up.
I've tried:
app.typeText("footer")
app.textViews.element(boundBy: 0).typeText("foobar")
app.links.element(boundBy: 0).typeText("foobar")
for some reason app.textViews.count always returns 0?
The problem is not that .typeText(_:) doesn't work, it's that your query doesn't resolve to an element.
It can sometimes seem like the query is not working properly when the view you're trying to find is inside an accessible container view of some sort. You can mitigate this by explicitly disabling accessibility in the container view.
Set the stack view that contains the text view to not be an accessibility element and then set an accessibility identifier on the text view.
// app code
let stack: UIStackView!
let textView: UITextView!
stack.isAccessibilityElement = false
textView.isAccessibilityElement = true
textView.accessibilityIdentifier = "myTextView"
// test code
let app = XCUIApplication()
let textView = app.textViews["myTextView"]
textView.typeText("something")
Try setting an unique accessibility identifier for your UITextView and then:
let textView = app.textViews["uniqueId"]
XCTAssert(textView.exists, "uniqueId text view NOT FOUND")
textView.tap()
textView.typeText("foobar")
Thanks for your attention.
I have an app that uses a UITableView as a timeline that shows certain events, The Cell prototype was a little complex because i use multiple labels, a button and some imageViews that automatically change in function of the content of other fields.
In that cell, I have a UILabel, this UILabel can have 140 characters or 4 line jumps, if the text inside the label have more line jumps (\n) or are longer that 140 chars, I take a fragment and only display that fragment and add the text "... READ MORE"; when the user taps on the text, the label change and shows all the text, and at the end, appends the label "READ LESS", If the user taps again the label, it return to the initial state showing the fragment and the label "READ MORE" and so.
When I test this, it works on a device with iOS9, but in devices with iOS 10 (including simulators) It stops to work; it appears that when I Tap the label, the label changes as usual, but immediately returns to their original form. I register only one tap.
Have an idea?, Here is my code that is called to update the cell when the user taps the text label:
func cellTextPressed(gesture: UITapGestureRecognizer){
let cell: TimeLineViewCell = gesture.view?.superview?.superview as! TimeLineViewCell
let tappedIndexPath: NSIndexPath = self.timelineTableView.indexPathForCell(cell)!
NSLog ("Text Tapped at: \(tappedIndexPath)")
if ((cell.isReasonExpanded) == true)
{
cell.isReasonExpanded = false
let attrs = [NSForegroundColorAttributeName: UIColor.magentaColor()]
let attributedReducedText = NSMutableAttributedString(string: cell.reducedReason)
attributedReducedText.appendAttributedString(NSAttributedString(string: "... "))
attributedReducedText.appendAttributedString(NSAttributedString(string: "READ MORE", attributes: attrs))
cell.labelReason.attributedText = attributedReducedText
}
else
{
cell.isReasonExpanded = true
let attrs = [NSForegroundColorAttributeName: UIColor.magentaColor()]
let attributedRealText = NSMutableAttributedString(string: cell.realReason)
attributedRealText.appendAttributedString(NSAttributedString(string: " "))
attributedRealText.appendAttributedString(NSAttributedString(string: "READ LESS", attributes: attrs))
cell.labelReason.attributedText = attributedRealText
}
let lastScrollOffset = self.timelineTableView.contentOffset
UIView.performWithoutAnimation
{
self.timelineTableView.beginUpdates()
self.timelineTableView.endUpdates()
self.timelineTableView.layer.removeAllAnimations()
self.timelineTableView.setContentOffset(lastScrollOffset, animated: false)
}
}
I already have a method to avoid this.In IOS 9 and older versions, there are no problem with that code; but in iOS 10 there is different.
In documentation, apple says that the delegate method tableView cellForRowAtIndexPath is called everytime when a cell is displayed on screen, for that cell. In the older versions of IOS, it appears to be a bug that make this situation don't work, let us modify the view outside of this method whitout callback cellForRowAtIndexPath after refresh the cell.
I Added an array of Bool values that represents the status of the labels (If it have extended or shrinked value of the label) and initialize it on the ItemsDownload Method of my JSON receiver method
I add a condition that evaluates the value in that stateArray in cellForRowAtIndexPath() and put the full or the fragment of the text as corresponds.
finally, in the method posted above, added the change of the value at index path to the status Array instead to change all in that method
I have a static numeric-keyboard made out of a bunch of buttons, I also have three UITextFields, textField1, textField2 and textField3 where I'm inputting the text using the static keyboard.
Here is the code I'm using to detect which textField is currently in focus and to input the content of the buttons. It kind of works but I don't like the fact that I have three IF statements and I'm not sure how to prevent the keyboard from appearing when a textField is tapped.
What would be the best way to implement this functionality?
#IBAction func appendKey(sender: AnyObject) {
let digit = sender.currentTitle!
if(textField1.isFirstResponder()){
textField1.text = textField1.text! + digit!
}else if(textField2.isFirstResponder()){
textField2.text = textField2.text! + digit!
}else if(textField3.isFirstResponder()){
textField3.text = textField3.text! + digit!
}
}
Thanks
If the standard keyboard is displaying then your custom keyboard isn't setup properly. Your custom keyboard should be the inputView of each UITextField. If you do that, the standard keyboard won't appear and yours will instead.
Your custom keyboard should be a separate class that handles all of it's own buttons. It appears you have everything in one view controller - all of the text fields, all of the buttons, and all of the button handling code. This is a bad approach. Create your custom keyboard class view. Put all of the code to handle and display the buttons in that custom view class. Create a single instance of this view in your view controller and assign the custom keyboard view instance to the inputView property of each text field.
In the custom keyboard class, listen for the UITextFieldTextDidBeginEditingNotification notification. This is how you keep track of the current text field. Your custom keyboard class should not have any specific reference to any text field other than track the current one. It should also ensure that the text field's inputView is itself.
In each button handler of the custom keyboard class, get the text you wish to append and then call the text field's insertText: method with the string. That's it. This will ensure the text is inserted and/or replaced based on the current selecting in the text field.
I have created a drop down with text and button, when the drop down button is clicked the data is populated in the UIAlertController of type action sheet.
Now to replicate drop down behavior need to set the text of textfield as the clicked UIAlertActon title.
But I am not able to find any way to fetch the title and set it as text of UITextField. I am using swift.
Please suggest.
When you create your UIAlertAction, you can add a handler which gets called when the button is tapped. Your action gets passed into the handler, so you can access its title property there:
let myAction = UIAlertAction(title: "Option 1", style: .Default) { action in
println(action.title)
}
You can see this in the docs for UIAlertAction.