質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1038閲覧

UIButtonのisEnabledを切り替えた際のタイトルの変化について( NSAttributedString/planeText )

退会済みユーザー

退会済みユーザー

総合スコア0

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/03/04 17:40

タイトルにNSAttributedStringを使ったUIButtonのisEnabledを切り替えた際の見た目の変化?をplaneText?をつかったUIButtonのそれと同じようにしたいのですが、なにか良い方法はありませんか? ご存知でしたら教えて下さい。

NSAttributedStringのほうがちらつくのが気になります。

左がNSAttributedString、右がplane
イメージ説明

swift

1class ViewController: UIViewController { 2 3 @IBOutlet weak var attTitleBtn: UIButton! 4 5 @IBOutlet weak var planeTitleBtn: UIButton! 6 7 let upperStr = "upperTitle" 8 9 let lowerStr = "lowerTitle" 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 15 // attTitleBtnSetting 16 attTitleBtn.titleLabel?.numberOfLines = 2 17 18 let style = NSMutableParagraphStyle() 19 style.lineSpacing = 4 20 style.alignment = .center 21 22 let upperAttrInInEnabled: [NSAttributedString.Key : Any] = [ 23 .foregroundColor: UIColor.systemBlue, 24 .font: UIFont.systemFont(ofSize: 14, weight: .semibold), 25 .paragraphStyle: style 26 ] 27 28 let lowerAttrInInEnabled: [NSAttributedString.Key : Any] = [ 29 .foregroundColor: UIColor.systemBlue, 30 .font: UIFont.systemFont(ofSize: 10, weight: .semibold), 31 .paragraphStyle: style 32 ] 33 34 let upperTitle = NSAttributedString(string: upperStr, attributes: upperAttrInInEnabled) 35 let lowerTitle = NSAttributedString(string: "\n(lowerStr)", attributes: lowerAttrInInEnabled) 36 37 let mutableAttributedStringInIsEnabled = NSMutableAttributedString() 38 39 mutableAttributedStringInIsEnabled.append(upperTitle) 40 mutableAttributedStringInIsEnabled.append(lowerTitle) 41 42 attTitleBtn.setAttributedTitle(mutableAttributedStringInIsEnabled, for: .normal) 43 44 45 let upperAttrInIsDisabled: [NSAttributedString.Key : Any] = [ 46 .foregroundColor: UIColor.lightGray, 47 .font: UIFont.systemFont(ofSize: 14, weight: .semibold), 48 .paragraphStyle: style 49 ] 50 51 let lowerAttrInIsDisabled: [NSAttributedString.Key : Any] = [ 52 .foregroundColor: UIColor.lightGray, 53 .font: UIFont.systemFont(ofSize: 10, weight: .semibold), 54 .paragraphStyle: style 55 ] 56 57 let upperTitleInIsDisabled = NSAttributedString(string: upperStr, attributes: upperAttrInIsDisabled) 58 let lowerTitleInIsDisabled = NSAttributedString(string: "\n(lowerStr)", attributes: lowerAttrInIsDisabled) 59 60 let mutableAttributedStringInIsDisabled = NSMutableAttributedString() 61 62 mutableAttributedStringInIsDisabled.append(upperTitleInIsDisabled) 63 mutableAttributedStringInIsDisabled.append(lowerTitleInIsDisabled) 64 65 attTitleBtn.setAttributedTitle(mutableAttributedStringInIsDisabled, for: .disabled) 66 67 // planeTitleBtnSetting 68 planeTitleBtn.titleLabel?.text = "planeTitleBtn" 69 planeTitleBtn.setTitleColor(UIColor.systemBlue, for: .normal) 70 planeTitleBtn.setTitleColor(UIColor.lightGray, for: .disabled) 71 72 } 73 74 // changeEnables 75 @IBAction func changeBtnsEnable(_ sender: UISwitch) { 76 77 //UIView.performWithoutAnimation { } 78 attTitleBtn.isEnabled = sender.isOn 79 planeTitleBtn.isEnabled = sender.isOn 80 81 } 82 83} 84

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Storyboard でボタンのタイプを Custom にすると良いようです。
https://stackoverflow.com/a/29509442/1019868

投稿2020/03/04 21:55

hoshi-takanori

総合スコア7895

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2020/03/05 05:30

回答ありがとうございます。 btnTypeをcusutom/.hightlightも設定して解決しました。 このプロパティって、getOnlyになってるんですが、コードから動的に変更できないのでしょうか? open var buttonType: UIButton.ButtonType { get }
退会済みユーザー

退会済みユーザー

2020/03/05 05:47

ありがとうございます。 initしかなくてセッタがないってことは、動的に変更されることは想定されてないわけですね…。
hoshi-takanori

2020/03/05 05:51

Objective-C にはクラスクラスターというものがあって、実は UIButton は抽象クラスで、type ごとにサブクラスに分かれてたんです。Swift でどうなってるかは把握してませんが…。
退会済みユーザー

退会済みユーザー

2020/03/05 05:58

回答ありがとうございます。 あー、初期化時点でtypeごとに別物があてがわれてる可能性があると… 納得がいく話ですね。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問