Guest User

Untitled

a guest
Oct 20th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. override func viewDidAppear(animated: Bool) {
  2. super.viewDidAppear(animated)
  3.  
  4. btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;
  5. }
  6.  
  7. @IBOutlet weak var btnTwoLine: UIButton?
  8.  
  9. override func viewDidAppear(animated: Bool) {
  10. super.viewDidAppear(animated)
  11.  
  12. //applying the line break mode
  13. btnTwoLine?.titleLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping;
  14.  
  15. var buttonText: NSString = "hellonthere"
  16.  
  17. //getting the range to separate the button title strings
  18. var newlineRange: NSRange = buttonText.rangeOfString("n")
  19.  
  20. //getting both substrings
  21. var substring1: NSString = ""
  22. var substring2: NSString = ""
  23.  
  24. if(newlineRange.location != NSNotFound) {
  25. substring1 = buttonText.substringToIndex(newlineRange.location)
  26. substring2 = buttonText.substringFromIndex(newlineRange.location)
  27. }
  28.  
  29. //assigning diffrent fonts to both substrings
  30. let font:UIFont? = UIFont(name: "Arial", size: 17.0)
  31. let attrString = NSMutableAttributedString(
  32. string: substring1 as String,
  33. attributes: NSDictionary(
  34. object: font!,
  35. forKey: NSFontAttributeName) as [NSObject : AnyObject])
  36.  
  37. let font1:UIFont? = UIFont(name: "Arial", size: 11.0)
  38. let attrString1 = NSMutableAttributedString(
  39. string: substring2 as String,
  40. attributes: NSDictionary(
  41. object: font1!,
  42. forKey: NSFontAttributeName) as [NSObject : AnyObject])
  43.  
  44. //appending both attributed strings
  45. attrString.appendAttributedString(attrString1)
  46.  
  47. //assigning the resultant attributed strings to the button
  48. btnTwoLine?.setAttributedTitle(attrString, forState: UIControlState.Normal)
  49.  
  50. }
  51.  
  52. let str = NSMutableAttributedString(string: "First linenSecond Line")
  53. str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 17), range: NSMakeRange(0, 10))
  54. str.addAttribute(NSFontAttributeName, value: UIFont.systemFont(ofSize: 12), range: NSMakeRange(11, 11))
  55. button.setAttributedTitle(str, for: .normal)
  56.  
  57. override func viewDidLoad() {
  58. super.viewDidLoad()
  59. var str = NSMutableAttributedString(string: "First linenSecond Line")
  60. str.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(17), range: NSMakeRange(0, 10))
  61. str.addAttribute(NSFontAttributeName, value: UIFont.systemFontOfSize(12), range: NSMakeRange(11, 11))
  62. button.setAttributedTitle(str, forState: .Normal)
  63.  
  64. }
  65.  
  66. let firstLabel = UILabel()
  67.  
  68. firstLabel.backgroundColor = UIColor.lightGrayColor()
  69. firstLabel.text = "Hi"
  70. firstLabel.textColor = UIColor.blueColor()
  71. firstLabel.textAlignment = NSTextAlignment.Center
  72. firstLabel.frame = CGRectMake(0, testButton.frame.height * 0.25, testButton.frame.width, testButton.frame.height * 0.2)
  73. testButton.addSubview(firstLabel)
  74.  
  75. let secondLabel = UILabel()
  76.  
  77. secondLabel.backgroundColor = UIColor.lightGrayColor()
  78. secondLabel.textColor = UIColor.blueColor()
  79. secondLabel.font = UIFont(name: "Arial", size: 12)
  80. secondLabel.text = "There"
  81. secondLabel.textAlignment = NSTextAlignment.Center
  82. secondLabel.frame = CGRectMake(0, testButton.frame.height * 0.5, testButton.frame.width, testButton.frame.height * 0.2)
  83. testButton.addSubview(secondLabel)
  84.  
  85. let button = UIButton()
  86. button.titleLabel?.numberOfLines = 0
  87. button.titleLabel?.lineBreakMode = .byWordWrapping
  88. button.setTitle("FoonBar", for: .normal)
  89. button.titleLabel?.textAlignment = .center
  90. button.sizeToFit()
  91. button.addTarget(self, action: #selector(rightBarButtonTapped), for: .allEvents)
  92. navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
Add Comment
Please, Sign In to add comment