NSMutableAttributedString as UIAlertAction's title - Swift - ios

Is it possible to set a NSMutableAttributedString as AlertAction's title?
My aim is to visualize a math expression with a good design in my UIAlertAction's title.
let action = UIAlertAction(title: myNSMutableAttributedString, style: UIAlertActionStyle.default) {(result : UIAlertAction) -> Void in
}
Thanks in advance for the help.

It's not possible to use an NSAttributedString for a UIAlertAction's title without accessing private APIs, as the method for setting the attributedTitle to an attributed string does not work here (see this question for details on that).
I would create a custom class or use an alert library for this.

Related

How can I add custom textField to my alert?

I can add textField to my alert as shown below using alert.addTextField()
let alert = UIAlertController(title: "Title", message: "Subtitle", preferredStyle: UIAlertController.Style.alert)
alert.addTextField()
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { _ in
print(alert.textFields?[0].text ?? "")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
However, if I have a custom textField, like CurrencyField as shared in https://stackoverflow.com/a/29783546/3286489, how could I add that to my alert (instead of just having a generic TextField)?
The Holy Path
The Apple-given API doesn't allow for you to use a custom subclass of UITextField. It does, however, allow you to customize the UITextField:
alert.addTextField { textField in
// customize text field
}
You'll have to try and port the functionality of your CurrencyView over, having only access to the APIs available by default on UITextField.
The Unholy Path
⚠️ Disclaimer: This is a Bad Idea. Using vendor APIs in ways they weren't intended to makes your code and application less stable.
Now that we've got that out of the way: you could also add a view directly to the default UITextField. You'd have to disable interaction/editing on the original UITextField, though, and make sure it only goes to your custom text field.
If you really wanted to go to the dark side, you could swizzle UITextField and force your CurrencyView to be initialized, but, once again, this is a Bad Idea.

Any way to make text in all buttons/action of a UIAlertController bold?

I have a requirement that the text in the buttons of a UIAlertController should be set to bold for every button (as opposed to the standard iOS behavior which is that the button assigned the style cancel is bold, or that for which preferredAction has been set is bold. The requirement is that all button text should be bold).
Is there a way to achieve this using a UIAlertController? Or will I be forced to created a custom dialog using a UIView?
There's plenty of past questions/answers on manipulating the body text for a UIAlertController using an attributed string, but I've not found anything for doing the equivalent for the text of the action buttons of an UIAlertController.
Buttons on UIAlertController are of UIAlertAction. There are no any setter methods on any property of UIAlertAction available, so you can't change the button text to bold.
Even title have to be String type, it doesn't accept NSAttributedString. So you should go with a custom dialog using a UIView.
Apple class reference for UIAlertAction:
#available(iOS 8.0, *)
open class UIAlertAction : NSObject, NSCopying {
public convenience init(title: String?, style: UIAlertActionStyle, handler: ((UIAlertAction) -> Swift.Void)? = nil)
open var title: String? { get }
open var style: UIAlertActionStyle { get }
open var isEnabled: Bool
}
Or will I be forced to created a custom dialog using a UIView?
Yes, that’s it. But it’s very easy, and gives you much more power and flexibility than UIAlertController. There are lots of sample projects out there, and once you’ve used one you may never go back to UIAlertController again!

How to make message of UIAlertController copyable in swift2, iOS9?

I've a simple question: How can I make the message of an UIAlertController be selectable and copyable by the user?
The controller is initiated like so:
let alertController = UIAlertController(title: "Hello World", message: "Copy Me!", preferredStyle: .Alert)
and displayed like so:
presentViewController(alertController, animated: true, completion: nil)
Adam's correct that UIAlertController doesn't provide text selection functionality, so a traditional copy/paste solution isn't going to work. You could alternatively provide a button on your UIAlertController that copies a string to the pasteboard.
UIPasteboard.general.string = "Copy Me!"
It is not possible. UIAlertController has no such functionality. It was implemented with UILabel components, which don't support copying text. You are not allowed to subclass UIAlertController either. The only option is to implement your own controller instead.

No initializer for UIAlertAction (title, style, handler)

Dear Stackoverflowers,
Can any experts on Swift/UIKit see what I'm doing wrong in the following screenshot?
Xcode can't seem to find the convenience initializer for a UIAlertAction based on a title, style and handler, and I don't know of any other way to initialize a UIAlertAction. In practice, the handler won't be nil.
Thank you in advance,
Jamie
You are trying to pass Int to style param which is against swift rule. Try this out:
let saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default, handler: nil)

How do i change the title of an existing UIAlertAction?

I have a like button in an action sheet:
likeAction = UIAlertAction(title: "Like this post", style: UIAlertActionStyle.Default, handler: { (action) -> Void in
println("like pressed")
})
Once pressed I'd like to update the title to "Unlike this post"
Title is readonly and there isn't a SetTitle() function on UIAlertActions.
Ideas much appreciated.
This works for me.
self.alertAction.setValue("New Title", forKeyPath: "title")
Hope this helps.
This was answered by Lyndsey Scott in a comment on the original post.
Set a bool variable to indicate whether the post is "liked" then set the alert title conditionally.

Resources