Change UIAlertController's title fontsize
Asked Answered
S

2

9

I'm trying to change the title fontSize in an UIAlertController, but I can't manage how to set my NSMutableAttributedString to the title-property.

So for I've been creating the NSMutableAttributedString with the following code:

let title = NSMutableAttributedString(string: user.fullName)
let range = NSRange(location: 0, length: title.length)
title.addAttribute(NSAttributedStringKey.font, value: UIFont.TextStyle.largeTitle, range: range)

Now the tricky part for me is how to figure out how to set the new title to the UIAlertController, because it's expecting a String? value.

I looked around and found out that I should probably create a UILabel within the completion block when presenting the UIAlertController. But how do I override the title-property in the UIAlertController with my own custom UILabel?

present(myUIAlertController, animated: true) {
    // Creating the UILabel depending on string length
    // Set the NSMutableAttributedString value to the custom UILabel and override title property.
}

Or maybe there's even an easier way to solve this?


My goal is to have like the image below:

UIAlertViewController with large title

Sorrells answered 30/8, 2018 at 11:24 Comment(4)
Can you use a UIView or UITableView like action sheet? It can be easily customize according to your requirement. Even custom action sheets are available.Watkin
here is the answer https://mcmap.net/q/173839/-uialertcontroller-custom-font-size-color in this string "alertController.setValue(attributedString, forKey: "attributedTitle")"Interracial
Making any change in any inbuilt control without using the inbuilt functions is not a good idea. It can be damage in future if  makes any change in that API. So better to make your own custom control.Watkin
Check this one github.com/urbn/URBNAlertGilletta
E
14

You can make title and message of UIAlertController attributed by using this code. You can customize as per your need. You can see the result in the image. I am not sure you can put it on Appstore.

func showAlert() {
  let alert = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)        
  let titleAttributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 25)!, NSAttributedStringKey.foregroundColor: UIColor.black]
  let titleString = NSAttributedString(string: "Name Last name", attributes: titleAttributes)     
  let messageAttributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!, NSAttributedStringKey.foregroundColor: UIColor.red]
  let messageString = NSAttributedString(string: "Company name", attributes: messageAttributes)
  alert.setValue(titleString, forKey: "attributedTitle")
  alert.setValue(messageString, forKey: "attributedMessage")
  let labelAction = UIAlertAction(title: "Label", style: .default, handler: nil)
  let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: nil)
  let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
  alert.addAction(labelAction)
  alert.addAction(deleteAction)
  alert.addAction(cancelAction)
  self.navigationController?.present(alert, animated: true, completion: nil)

}

enter image description here

Equity answered 30/8, 2018 at 12:18 Comment(0)
S
0

There is no way except using of private API.

I can suggest you to make your AlertViewController with properties that you want to customize.

Spawn answered 30/8, 2018 at 11:43 Comment(2)
Private APIs should be avoided, since apps might be rejected by Apple upon release.Proleg
Yes, I fully agree with you. Using of private API is the reason to reject your app by Apple. But you can use it for educational purpose or something like that.Spawn

© 2022 - 2024 — McMap. All rights reserved.