美文网首页
iOS OC AlertViewController messa

iOS OC AlertViewController messa

作者: 林希品 | 来源:发表于2024-06-17 09:25 被阅读0次
    //提示更新    更新类型:1-强制更新 2-提醒更新 3-可忽略更新
                        NSString *version_name = [NSString stringWithFormat:@"%@ %@", @"发现最新版本, 请下载", [data valueForKey:@"updateDescription"]];
    //                    NSString *version_name = [NSString stringWithFormat:@"%@", @"发现最新版本, 请下载"];
                        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"检查更新" message:version_name preferredStyle:(UIAlertControllerStyleAlert)];
                        // Create a mutable paragraph style
                        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
                        paragraphStyle.alignment = NSTextAlignmentLeft;
    
                        // Create an attributed string with the message text and the paragraph style
                        NSString *messageText = version_name;
                        NSDictionary *attributes = @{
                            NSParagraphStyleAttributeName: paragraphStyle,
                            NSFontAttributeName: [UIFont systemFontOfSize:13.0] // Customize the font size if needed
                        };
    
                        NSAttributedString *attributedMessage = [[NSAttributedString alloc] initWithString:messageText attributes:attributes];
    
                        // Set the attributed message to the alert controller
                        [alertVC setValue:attributedMessage forKey:@"attributedMessage"];
                        
                        UIAlertAction *right = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                            NSLog(@"点击更新了");
                            NSString *safari_url = [data valueForKey:@"url"];
                            if (safari_url.length > 0) {
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:safari_url]]) {
                                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:safari_url] options:@{} completionHandler:^(BOOL success) {
                                            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                exit(0);
                                            });
                                        }];
                                    }
                                });
                            }
                        }];
                        
                        UIAlertAction *left = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            NSLog(@"取消");
                        }];
                        [alertVC addAction:right];
                        [alertVC addAction:left];
                        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:^{
                            
                        }];
                    }
    

    解释
    创建 UIAlertController:

    objective
    复制代码
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:nil preferredStyle:UIAlertControllerStyleAlert];
    创建一个 UIAlertController 实例,指定标题、消息为空(因为我们会使用富文本消息),以及警告样式。

    创建段落样式并设置左对齐:

    objective
    复制代码
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    创建一个 NSMutableParagraphStyle 实例,并将文本对齐方式设置为左对齐。

    创建富文本消息:

    objective
    复制代码
    NSString *messageText = @"This is a sample message that should be left aligned.";
    NSDictionary *attributes = @{
    NSParagraphStyleAttributeName: paragraphStyle,
    NSFontAttributeName: [UIFont systemFontOfSize:13.0] // Customize the font size if needed
    };
    NSAttributedString *attributedMessage = [[NSAttributedString alloc] initWithString:messageText attributes:attributes];
    创建一个包含消息文本的 NSAttributedString,并应用段落样式和其他属性(如字体大小)。

    将富文本消息设置到 UIAlertController:

    objective
    复制代码
    [alertController setValue:attributedMessage forKey:@"attributedMessage"];
    使用 KVC (Key-Value Coding) 将富文本消息设置为 UIAlertController 的 attributedMessage 属性。

    添加操作并显示警告控制器:

    objective
    复制代码
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
    添加一个默认样式的“OK”操作,并显示警告控制器。

    相关文章

      网友评论

          本文标题:iOS OC AlertViewController messa

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