UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"標題" message:@"內(nèi)容" preferredStyle:UIAlertControllerStyleAlert];
// 使用富文本來改變alert的title字體大小和顏色
NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:@"這里是標題"];
[titleText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:24] range:NSMakeRange(0, 2)];
[titleText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[alert setValue:titleText forKey:@"attributedTitle"];
// 使用富文本來改變alert的message字體大小和顏色
// NSMakeRange(0, 2) 代表:從0位置開始 兩個字符
NSMutableAttributedString *messageText = [[NSMutableAttributedString alloc] initWithString:@"這里是正文信息"];
[messageText addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, 6)];
[messageText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 2)];
[messageText addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:NSMakeRange(3, 3)];
[alert setValue:messageText forKey:@"attributedMessage"];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 設(shè)置按鈕背景圖片
UIImage *accessoryImage = [[UIImage imageNamed:@"selectRDImag.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[cancelAction setValue:accessoryImage forKey:@"image"];
// 設(shè)置按鈕的title顏色
[cancelAction setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"];
// 設(shè)置按鈕的title的對齊方式
[cancelAction setValue:[NSNumber numberWithInteger:NSTextAlignmentLeft] forKey:@"titleTextAlignment"];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:okAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
UIAlertController 私有 api 列表及其類型:
_message, @"NSString"
_attributedTitle, @"NSAttributedString"
_attributedMessage, @"NSAttributedString"
_attributedDetailMessage, @"NSAttributedString"
_linkedAlertControllers, @"NSSet"
_cancelAction, @"UIAlertAction"
_actionToKeyCommandsDictionary,
_keyCommandToActionMapTable, @"NSMapTable"
_resolvedStyle, q
_preferredStyle, q
_contentViewController, @"UIViewController"
_textFieldViewController,
_backButtonDismissGestureRecognizer,
_ownedTransitioningDelegate, @
_addContentViewControllerToViewHierarchyNeeded, B
_isInSupportedInterfaceOrientations, B
_isInRecomputePreferredContentSize, B
_batchActionChangesInProgressCount, q
_presenter, @"_UIAlertControllerShimPresenter"
_actionsWithInvokedHandlers, @"NSPointerArray"
_alertControllerStackManager,
_hidden, B
_springLoaded, B
__shouldFlipFrameForShimDismissal, B
__shouldAllowNilParameters, B
_hasPreservedInputViews, B
_actions, @"NSMutableArray"
_headerContentViewController, @"UIViewController"
_separatedHeaderContentViewController,
_styleProvider,
_preferredAction, @"UIAlertAction"
_temporaryAnimationCoordinator,
_previewInteractionController,
__visualStyle, @"UIAlertControllerVisualStyle"
_indexesOfActionSectionSeparators, @"NSIndexSet"
__actionDelimiterIndices, @"NSMutableArray"
__compatibilityPopoverController,
__systemProvidedPresentationView, @"UIView"
__systemProvidedPresentationDelegate,
_systemProvidedGestureRecognizer,
_coordinatedActionPerformingDelegate,
__presentationSourceRepresentationView, @"UIView"
_titleMaximumLineCount, q
_titleLineBreakMode, q
UIAlertAction 私有 api 列表及其類型:
_title, @"NSString"
_titleTextAlignment, q
_enabled, B
_checked, B
_isPreferred, B
_imageTintColor, @"UIColor"
_titleTextColor, @"UIColor"
_style, q
_handler, @?
_simpleHandler, @?
_image, @"UIImage"
_shouldDismissHandler, @?
__descriptiveText, @"NSString"
_contentViewController, @"UIViewController"
_keyCommandInput, @"NSString"
_keyCommandModifierFlags, q
__representer,
__interfaceActionRepresentation, @"UIInterfaceAction<UIAlertActionMutablePropertyObservering>"
__alertController, @"UIAlertController"