Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertcontroller set attributed messages

How we can set Attributed text in UIAlertcontroller as message. My try code As bellow, but it will crash the app.

// create Attributed text

let myAttribute = [NSForegroundColorAttributeName: UIColor(red:122.0/255, green:125.0/255, blue:131.0/255, alpha:1.0),NSFontAttributeName: Constant.flinntRegularFont(15)]
let myAttribute2 = [NSForegroundColorAttributeName: UIColor.blackColor(),NSFontAttributeName: Constant.flinntMediumFont(15)]

let myString = NSMutableAttributedString(string: "You have been unsubscribed from ", attributes: myAttribute)
let myString2 = NSMutableAttributedString(string: self.course.course_name, attributes: myAttribute2)
let myString3 = NSMutableAttributedString(string: "\n\nYour refund will be initiated within one week.", attributes: myAttribute)
let myString4 = NSMutableAttributedString(string: "\n\nFor any help call us on", attributes: myAttribute)
let myString5 = NSMutableAttributedString(string: " 079-4014 9800", attributes: myAttribute2)
let myString6 = NSMutableAttributedString(string: " between 9:30 am to 6:30 pm on Monday to Saturday.\n\nWe will be always here with great deals to share.", attributes: myAttribute)

myString.appendAttributedString(myString2)
myString.appendAttributedString(myString3)
myString.appendAttributedString(myString4)
myString.appendAttributedString(myString5)
myString.appendAttributedString(myString6)

Present UIAlertcontroller Code

let alert = UIAlertController(title: "", message: "Select course", preferredStyle: UIAlertControllerStyle.Alert)
    alert.setValue(myAttribute, forKey: "attributedMessage") // this line make a crash.

    alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: { (action) in
        self.delegate?.courseRefundViewControllerCoursrRefunded?(self)
        self.navigationController?.popViewControllerAnimated(true)
    }))
self.presentViewController(alert, animated: true, completion: nil)

Thanks.

enter image description here

like image 748
jignesh Vadadoriya Avatar asked May 23 '17 09:05

jignesh Vadadoriya


People also ask

How do I create an attributed string?

Creating Attributed Strings with Markdown You can create an attributed string by passing a standard String or Data instance that contains Markdown to initializers like init(markdown:options:baseURL:) . The attributed string creates attributes by parsing the markup in the string.

How does UIAlertController work?

The alert controller lets you provide a block for configuring your text fields prior to display. The alert controller maintains a reference to each text field so that you can access its value later. The UIAlertController class is intended to be used as-is and doesn't support subclassing.

How do I create an alert view in Swift?

To create an action button that the user can tap on, we will need to create a new instance of an UIAlertAction class and add it to our alert object. To add one more button to UIAlertController simply create a new UIAlertAction object and add it to the alert.


1 Answers

You app is crashing because of DataType mismatch.

alert.setValue(<value>, forKey: "attributedMessage")

Here <value> must be an instance of NSMutableAttributedString.

But you are passing myAttribute Which is Dictionary.

It is trying ta call length method but it is not found on Dictionary thats why app is crashing.

Try this:

alert.setValue(myString, forKey: "attributedMessage")
like image 73
CRDave Avatar answered Sep 23 '22 07:09

CRDave