teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

メッセージ部分にNSAttributedStringを設定する方法を追加しました

2020/05/22 01:33

投稿

ch3cooh
ch3cooh

スコア287

answer CHANGED
@@ -1,4 +1,4 @@
1
- 可能か不可能かでいえば可能です。ただUIAlertControllerを使うのではなく、目的に合致するようにカスタマイズしたアラートダイアログ画面を実装した方が良いかと思います。
1
+ 可能か不可能かでいえば可能です。ただ UIAlertController を使うのではなく、目的に合致するようにカスタマイズしたアラートダイアログ画面を実装した方が良いかと思います。
2
2
 
3
3
  ---
4
4
 
@@ -33,6 +33,30 @@
33
33
 
34
34
  ![イメージ説明](b1cb95fd63d25978f57fb6faa6da12f5.png)
35
35
 
36
+ メッセージ部分にNSAttributedStringを差し込む場合は下記の通りです。
37
+
38
+ ```
39
+ let alert = UIAlertController(title: "タイトル", message: "メッセージ", preferredStyle: .alert)
40
+ alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
41
+
42
+ let message = NSMutableAttributedString()
43
+ message.append(NSAttributedString(string: "テスト →"))
44
+
45
+ let attachment = NSTextAttachment()
46
+ attachment.image = UIImage(systemName: "person")
47
+ attachment.bounds.origin = CGPoint(x: 0, y: 0)
48
+ attachment.bounds.size = CGSize(width: 10, height: 10)
49
+ message.append(NSAttributedString(attachment: attachment))
50
+
51
+ message.append(NSAttributedString(string: "← テスト"))
52
+
53
+ alert.setValue(message, forKey: "attributedMessage")
54
+
55
+ present(alert, animated: true, completion: nil)
56
+ ```
57
+
58
+ ![イメージ説明](ad9b3ae5bbea504897f49211e04e248e.png)
59
+
36
60
  ### 動作確認環境
37
61
 
38
62
  下記の環境で動作検証をおこないました。