5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

何回閉じても無駄ですよ in Swift

Posted at

むしゃくしゃしてやった、後悔はしていない.

mudadesuyo.gif

作り方

次のようなビューコントローラを作る.

AsciiAlertController.swift
import UIKit

class AsciiAlertController: UIAlertController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        for label in findLabels(in: view) {
            label.textAlignment = .left
            label.font = UIFont.monospacedDigitSystemFont(ofSize: label.font.pointSize, weight: .regular)
        }
    }
    
    func findLabels(in view: UIView) -> [UILabel] {
        return view.subviews.flatMap({ subview -> [UILabel] in
            if let label = subview as? UILabel {
                return [label] + findLabels(in: label)
            } else {
                return findLabels(in: subview)
            }
        })
    }
}

次のように呼び出す

ViewController.swift
    func showAlert() {
        let message = """
 ∧_∧ ババババ
( ・ω・)=つ≡つ
(っ ≡つ=つ
`/  )
(ノΠU
何回閉じても無駄ですよ?ww
m9(^Д^)プギャー!!
"""

        let dialog = AsciiAlertController(title: nil, message: message, preferredStyle: .alert)
        dialog.addAction(UIAlertAction(title: "閉じる", style: .default, handler: { _ in
            self.showAlert()
        }))
        show(dialog, sender: nil)
    }

工夫したところ

  • AAを表示するために,UIAlertViewControllerのUILabelを全部左寄せにした
  • また,等幅フォントにした(これはやらなくても変わらなかったかも)

環境

  • Xcode: 10.1
  • macOS 10.14 (Mojave)
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?