美文网首页
UIAlertController 自定义

UIAlertController 自定义

作者: kangomake | 来源:发表于2021-10-21 17:22 被阅读0次
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"内容" 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];
    
    // 设置按钮背景图片
    UIImage *accessoryImage = [[UIImage imageNamed:@"selectRDImag.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [cancelAction setValue:accessoryImage forKey:@"image"];
    
    // 设置按钮的title颜色
    [cancelAction setValue:[UIColor lightGrayColor] forKey:@"titleTextColor"];
    
    // 设置按钮的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];
    
效果图.png
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"

相关文章

网友评论

      本文标题:UIAlertController 自定义

      本文链接:https://www.haomeiwen.com/subject/vqtqaltx.html