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.
Related
let newLabel = UILbael()
let button = UIButton()
button.setValue(newLabel, forKeyPath: "titleLabel")
crash info
setValue:forUndefinedKey:]: this class is not key valuecoding-compliant for the key titleLabel
how do it if use kvc ?
You should use setTitle method to set button title for states.
button.setTitle("Your button title here", for: .normal)
setValue(_:forKeyPath:) is a method from NSObject class which UIButton is a subclass of. It is not recommended to use KVO. Read this thread for more information.
KVC is only supported for NSObjects, and Apple seems to be phasing it out in Swift. I don't recommend using KVC for new development.
You also shouldn't use the button's titleLabel to set the button's title. To quote the Apple docs on UIButton:
To set the actual text of the label, use setTitle(_:for:)
(button.titleLabel.text does not let you set the text).
If you have two buttons, firstButton and secondButton, and you're trying to copy the title from the first to the second, your code might look like this:
let title = firstButton.title(forState: .normal)
secondButton.setTitle(title, for: .normal)
I think u have not confirmed your outlet in your ViewController that's a reason when use button and set title there are not any references of a button in ViewController. it's like the use of any object without any initialize. So make sure and check button outlet. Please see code so we make sure what is actual problem.
I am using a UITextView to make hashtags selectable. It detects taps perfectly. My issue is with the selection color. It looks black and I wanted the text to fade a bit on selection instead.
Here's what I've got now:
I've tried changing the tintColor, the NSForegroundColorAttributeName, and the NSBackgroundColorAttributeName but it doesn't work.
There is no documented property for the selected or highlighted color of detected links, but you should be able to achieve the same effect by overriding the delegate method textView:shouldInteractWithURL:inRange: and changing the color yourself.
From the UITextViewDelegate protocol reference:
The text view calls this method if the user taps or long-presses the
URL link. Implementation of this method is optional. By default, the
text view opens the application responsible for handling the URL type
and passes it the URL. You can use this method to trigger an
alternative action, such as displaying the web content at the URL in a
web view within the current application.
The last parameter is an NSRange object called characterRange, which represents the character range containing the tapped URL (or hashtag). Using that range, you should be able to add attributes such as NSForegroundColorAttributeName so as to change only the color of the particular hashtag that was tapped.
You'll probably want to revert any changes on touchesEnded and touchesCancelled.
Alternatively, you could make your own subclass and manually handle the above.
Although there is no public API to do this, I was curious and decided to dig through the private header for UITextField. I found that there is a class method on UITextField _sharedHighlightView which returns an instance of the private _UIHighlightView class. This is the class in charge of the highlighting. Swizzling _sharedHighlightView and changing its color will allow you to change the color of any data-detected links:
WARNING: This is a hack that uses method swizzling and private APIs/properties.
class MyTextView: UITextView {
var newHighlightView: AnyObject?
func changeHighlight() {
self.newHighlightView = UITextView.performSelector(Selector("_sharedHighlightView")).takeUnretainedValue()
self.newHighlightView?.setValue(UIColor.redColor().colorWithAlphaComponent(0.6), forKey: "_color")
let originalHighlightView = class_getClassMethod(MyTextView.self, Selector("_sharedHighlightView"))
let newHighlightView = class_getClassMethod(MyTextView.self, #selector(MyTextView.swizzleHighlightView))
method_exchangeImplementations(originalHighlightView, newHighlightView)
}
func swizzleHighlightView() -> AnyObject {
return self.newHighlightView!
}
}
In your View Controller:
let textView = MyTextView(frame: CGRect(x: 0, y: 40.0, width: 200.0, height: 200.0))
textView.dataDetectorTypes = .All
textView.editable = false
textView.text = "Some text. http://www.google.com/"
textView.changeHighlight()
self.view.addSubview(textView)
Result:
This could probably be cleaned up a little further by not force-unwrapping any optionals.
Work around for this can be done, you can use UIButton instead of textview. By using button you can have the same effect.
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."
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.
I'm learning OBJ.C/Xcode/Cocoa here and have run into a question I can't seem to find an answer for.
I want the text in a UILabel to switch between two states (ON/OFF for example) each time the user clicks the same button.
I can get the label to switch to one state, but can't get it to switch "back" when the user clicks the button again. I am assuming there is some logic required, or checking the status of the object once the button is clicked...
Does this require me to keep track of a bool or the "state" of the Label (or button)?
Would this require two methods to be associated to the button, or can it happen with just one?
Thanks for any guidance/pointing in the right direction/Code snips!!!!
~Steve
I got an answer, and it works:
- (IBAction)flip:(id)sender
{
_flipButton.selected = !_flipButton.selected;
self.flipLabel.text = (_flipButton.selected) ? #"ON" : #"OFF";
}
- (IBAction)flip:(id)sender {
[yourButton setSelected:!yourButton.selected];
self.flipLabel.text = (yourButton.selected) ? #"ON" : #"OFF";
}
This toggles the selected state of the button then checks its state to determine which string to pass to the text property of your label.
the easiest way (I think) is to store a BOOL with the state of the UILabel and (on each click of the button) negate the BOOL and set the appropriate text of the Label
You could call this: yourLabel.enabled = !yourLabel.enabled on button click to change the enabled-state of your UILabel. Or what kind of state do you mean ?