Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func viewDidAppear(animated: Bool) {
- super.viewDidAppear(animated)
- btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;
- }
- @IBOutlet weak var btnTwoLine: UIButton?
- override func viewDidAppear(animated: Bool) {
- super.viewDidAppear(animated)
- //applying the line break mode
- btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;
- var buttonText: NSString = "hellonthere"
- //getting the range to separate the button title strings
- var newlineRange: NSRange = buttonText.rangeOfString("n")
- //getting both substrings
- var substring1: NSString = ""
- var substring2: NSString = ""
- if(newlineRange.location != NSNotFound) {
- substring1 = buttonText.substringToIndex(newlineRange.location)
- substring2 = buttonText.substringFromIndex(newlineRange.location)
- }
- //assigning diffrent fonts to both substrings
- let font:UIFont? = UIFont(name: "Arial", size: 17.0)
- let attrString = NSMutableAttributedString(
- string: substring1 as String,
- attributes: NSDictionary(
- object: font!,
- forKey: NSFontAttributeName) as [NSObject : AnyObject])
- let font1:UIFont? = UIFont(name: "Arial", size: 11.0)
- let attrString1 = NSMutableAttributedString(
- string: substring2 as String,
- attributes: NSDictionary(
- object: font1!,
- forKey: NSFontAttributeName) as [NSObject : AnyObject])
- //appending both attributed strings
- attrString.appendAttributedString(attrString1)
- //assigning the resultant attributed strings to the button
- btnTwoLine?.setAttributedTitle(attrString, forState: UIControlState.Normal)
- }
- let str = NSMutableAttributedString(string: "First linenSecond Line")
- str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 17), range: NSMakeRange(0, 10))
- str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(11, 11))
- button.setAttributedTitle(str, for: .normal)
- override func viewDidLoad() {
- super.viewDidLoad()
- var str = NSMutableAttributedString(string: "First linenSecond Line")
- str.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(17), range: NSMakeRange(0, 10))
- str.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: NSMakeRange(11, 11))
- button.setAttributedTitle(str, forState: .Normal)
- }
- let firstLabel = UILabel()
- firstLabel.backgroundColor = UIColor.lightGrayColor()
- firstLabel.text = "Hi"
- firstLabel.textColor = UIColor.blueColor()
- firstLabel.textAlignment = NSTextAlignment.Center
- firstLabel.frame = CGRectMake(0, testButton.frame.height * 0.25, testButton.frame.width, testButton.frame.height * 0.2)
- testButton.addSubview(firstLabel)
- let secondLabel = UILabel()
- secondLabel.backgroundColor = UIColor.lightGrayColor()
- secondLabel.textColor = UIColor.blueColor()
- secondLabel.font = UIFont(name: "Arial", size: 12)
- secondLabel.text = "There"
- secondLabel.textAlignment = NSTextAlignment.Center
- secondLabel.frame = CGRectMake(0, testButton.frame.height * 0.5, testButton.frame.width, testButton.frame.height * 0.2)
- testButton.addSubview(secondLabel)
- let button = UIButton()
- button.titleLabel?.numberOfLines = 0
- button.titleLabel?.lineBreakMode = .byWordWrapping
- button.setTitle("FoonBar", for: .normal)
- button.titleLabel?.textAlignment = .center
- button.sizeToFit()
- button.addTarget(self, action: #selector(rightBarButtonTapped), for: .allEvents)
- navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
Add Comment
Please, Sign In to add comment