0

I have this function for adding a bullet to a textView

let bullet: String = " ● "

    func setAttributedValueForBullets(bullet: String, positionWhereTheCursorIs: Int?) {

    var textRange = selectedRange
    let selectedText = NSMutableAttributedString(attributedString: attributedText)

    if let line = positionWhereTheCursorIs {
        textRange.location = line
    }

    selectedText.mutableString.replaceCharacters(in: textRange, with: bullet)

    let paragraphStyle = createParagraphAttribute()
    selectedText.addAttributes([NSParagraphStyleAttributeName: paragraphStyle], range: NSMakeRange(textRange.location, bullet.length))

    self.attributedText = selectedText
    self.selectedRange = textRange
}

and it works when inserting a bullet to a paragraph with just one line like this

bullet with just one single line

but when I add it to a paragraph with more than one line this happen bullet with more than one line

I want it to look like in the first image, without that space in the bullet and the begining of text

I have also tried to use selectedText.insert(bullet, at: textRange.location)

instead of selectedText.addAttributes([NSParagraphStyleAttributeName: paragraphStyle], range: NSMakeRange(textRange.location, bullet.length))

dmram
  • 125
  • 1
  • 12
  • Could it be related to a breakline mode? If you write words with spaces instead of a long string with no space between letters, does it happens too? – Larme Jun 13 '17 at 18:40

2 Answers2

0
func attributeString() -> NSAttributedString{
        let attribute = NSMutableAttributedString(string: "●DESCRIPTION\n", attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 14)])
        let style = NSMutableParagraphStyle()
        style.lineSpacing = 10
        let range = NSMakeRange(0, attribute.string.characters.count)
        attribute.addAttribute(NSParagraphStyleAttributeName, value: style, range: range)

        return attribute
    }
Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38
0

This is essentially happening because of the space between the bullet point and the long description. If the description is too long, it will, by normal behavior, go on to it's own line, wrapping the rest of the string.

The easiest fix for this, is to use a non-breaking unicode space character (u00A0):

let bullet = "●\u{00A0}"
let string = "thisisaveryveryveryveryveryveryveryveryveryverylongstring"
textView.text = bullet + string

This will result in the expected behavior, where the very long string will not break into it's own line, as it will be connected to the preceding bullet point:

textView

Also, if the space between the bullet and the string is too small, simply string together multiple non-breaking spaces:

let bullet = "●\u{00A0}\u{00A0}"
jacob
  • 3,507
  • 2
  • 21
  • 26
  • Thank you very much this fixed that error, but know i have one with the tabs, when the user press a tab and then a bullet the same mistake happen, i need to replace the tab also with a non-breaking unicode tab? – dmram Jun 13 '17 at 21:23
  • In that case, I would just combine multiple non-breaking spaces. In the case of a tab, it is usually 4 spaces, thus 4 non-breaking spaces should do. If this fixed your issue please accept as the answer. Thank you :) – jacob Jun 14 '17 at 00:31