タイトルにNSAttributedString
を使ったUIButton
のisEnabledを切り替えた際の見た目の変化?をplaneText?
をつかったUIButton
のそれと同じようにしたいのですが、なにか良い方法はありませんか? ご存知でしたら教えて下さい。
NSAttributedString
のほうがちらつくのが気になります。
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/03/05 05:30
2020/03/05 05:36
退会済みユーザー
2020/03/05 05:47
2020/03/05 05:51
退会済みユーザー
2020/03/05 05:58