Method of setting user defined title and content in uialertcontroller of IOS

Time:2021-4-26

preface

I believe you all know that the title and content of uialertcontroller are black (for those who don’t know uialertcontroller, please refer to this article). However, in many scenarios, you need to modify their colors, such as changing the prompt information to red when an input error occurs, or customizing the color of the title. However, there seems to be no corresponding method in the open API interface, So what should we do? Let’s take a look at the detailed introduction

Third party controls

The first method, of course, is to use the third-party alert control. Now there are many alert controls (such as sclallertview, etc.) on GitHub. I believe many of them can meet your needs. Just use cocoapods to add a third-party library.

KVC method

But there are also some people who are not willing to use the third-party library, but want to use the UIalert controller of the system. Of course, this is OK. Apple did not completely block the customization of UIalert controller, but modified it to use KVC method for customization. If you want to customize the title and content, you can set the font and color through nsattributedstring, and then set them through KVC.

Here is a sample code and the corresponding screenshot:

- (void)testAlert {
 UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
 [alert Addaction: [UIalert action actionwithtitle: @ "Cancel" style:UIAlertActionStyleCancel handler :nil]];
 NSDictionary *titleAttr = @{
  NSFontAttributeName:[UIFont boldSystemFontOfSize:20],
  NSForegroundColorAttributeName:[UIColor greenColor]
  };
 Nsattributedstring * attributedtitle = [[nsattributedstring alloc] initwithstring: @ "test with color title" a ttributes:titleAttr ];
 [alert setValue:attributedTitle forKey:@"attributedTitle"];

 NSDictionary *messageAttr = @{
  NSFontAttributeName:[UIFont systemFontOfSize:12],
  NSForegroundColorAttributeName:[UIColor redColor]
  };
 Nsattributedstring * attributedmessage = [[nsattributedstring alloc] initwithstring: @ "test colored text" a ttributes:messageAttr ];
 [alert setValue:attributedMessage forKey:@"attributedMessage"];
 [self presentViewController:alert animated:YES completion:nil];
}

Screen capture

That’s all about the custom title and content. The main thing is to see the code.

summary

The above is the whole content of this article, there are many deficiencies in this article, I hope the content of this article has a certain reference learning value for your study or work, if you have any questions, you can leave a message to exchange, thank you for your support for developer.

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]