I want to some part of the text in UIButton as Bold and other and other part of it as plain. Also I want to put bolded text in one line and the plain text in the other. Can we accomplish with UIButton in Swift out of the box. Or should we make a custom cell/view that fits my need.
Asked
Active
Viewed 98 times
0
-
@rmaddy This is not a duplicate. In addition, to multiple lines, I 'm also asking about applying different fonts/ styling to different pieces of text. So, kindly remove the mis-leading duplicate tag. – Vineel Mar 17 '19 at 02:46
-
If you read through the Swift answers in the duplicate, it shows how to apply different fonts to different parts of the multi-line label. – rmaddy Mar 17 '19 at 02:51
-
That doesn't mean the question is duplicate. – Vineel Mar 17 '19 at 06:05
-
1Why isn't it a duplicate? Both your question and the other ask the same thing - how to create a button with two lines of text each using a different font. Same question. The answers in the other provide solutions to both questions. Please explain why you don't think it's a duplicate. And it's not bad to mark your question a duplicate. It helps other people find an answer. It's a good thing. – rmaddy Mar 17 '19 at 06:16
1 Answers
2
Yes, here is an example playground. Note you need to set an attributed title and enable word wrapping or it won't work.
import UIKit
import PlaygroundSupport
class ViewController: UIViewController {
let button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let text = NSMutableAttributedString(string: "Line 1\n", attributes: [NSMutableAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .bold)])
text.append(NSAttributedString(string: "Line 2", attributes: [NSMutableAttributedString.Key.font: UIFont.systemFont(ofSize: 11, weight: .regular)]))
button.setAttributedTitle(text, for: .normal)
button.titleLabel?.lineBreakMode = .byWordWrapping
button.sizeToFit()
view.addSubview(button)
}
}
PlaygroundPage.current.liveView = ViewController()

Josh Homann
- 15,933
- 3
- 30
- 33