0

I have a string Ex: Arya said "We must find a solution" yesterday. Here I want to restrict the width of the text in bold ("We must find a solution") to 100, but others must not be shortened. So I added it as 3 different NSMutableAttributedStrings appended together and set this to a UILabel. I want to know if I can restrict the width of one of these strings alone. I tried the following :

let mutableString = "We must find a solution"

  1. mutableString.draw(in: CGRect(x: 0, y: 0, width: 100, height: 30))

  2. mutableString.draw(with: CGRect(x: 0, y: 0, width: 10, height: 10), options: .truncatesLastVisibleLine, context: .none)

  3. mutableString.boundingRect(with: CGSize(width: 100, height: 40), options: [.truncatesLastVisibleLine,.usesFontLeading], context: .none)

But none of them worked. I want to reproduce the UILabel .lineBreakMode = .byTruncatingTail for a NSMutableAttributedString. Am I doing something wrong here, is there a way to achieve this?

koen
  • 5,383
  • 7
  • 50
  • 89
KP1
  • 69
  • 7
  • What should happen if the bold string is longer than 100? Do you want to cut it off at the end? Can you add some pictures showing the end result for different bold string lengths? – koen Aug 27 '20 at 12:40
  • Ya I want the last part to be truncated like this : This is a truncat.... – KP1 Aug 27 '20 at 12:52
  • 1
    "But none of them worked" - what exactly doesn't work? – koen Aug 27 '20 at 15:07
  • I wanted the resulting NSMutableAttributedString to be : "We must find.... so I tried the methods 1,2,3 listed above but the string returned is always the non truncated version. I want to reproduce the UILabel's .lineBreakMode = .byTruncatingTail for a NSMutableAttributedString – KP1 Aug 27 '20 at 16:08
  • 1
    How about using three `UILabel`s next to each other? Now you can use `.lineBreakMode` for the bold text. You can line them up in a horizontal `UIStackView` to make it look like one string. – koen Aug 27 '20 at 18:39
  • No I think it works seperately but not when appended to other `NSMutableAttributedString` – KP1 Aug 27 '20 at 19:09

1 Answers1

1

Here is a solution using three UILabels in a UIStackView:

override func viewDidLoad() {
    super.viewDidLoad()

    let label1 = UILabel()
    label1.text = "Arya said"
    label1.font = UIFont.italicSystemFont(ofSize: 20.0)

    let label2 = UILabel()        
    label2.text = "\"We must find a solution\""
    label2.font = UIFont.boldSystemFont(ofSize: 20.0)
    label2.lineBreakMode = .byTruncatingTail
    
    let label3 = UILabel()
    label3.text = "yesterday"
    label3.font = UIFont.italicSystemFont(ofSize: 20.0)
    
    let stack = UIStackView(arrangedSubviews: [label1, label2, label3])
    
    stack.axis = .horizontal
    stack.spacing = 4
    
    label2.translatesAutoresizingMaskIntoConstraints = false
    stack.translatesAutoresizingMaskIntoConstraints = false
    
    NSLayoutConstraint.activate([
        label2.widthAnchor.constraint(equalToConstant: 100)
    ])
    
    self.view.addSubview(stack)
    
    NSLayoutConstraint.activate([
        stack.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
        stack.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
    ])
}

output:

enter image description here

koen
  • 5,383
  • 7
  • 50
  • 89