5

How can I make a NSAttributedString that would basically be a horizontal separator, like HTML's <hr> tag?

I've tried:

NSAttributedString *hr = [[NSAttributedString alloc] initWithHTML:
  [NSData
    dataWithBytes:"<html><body><hr>hi</body></html>"
    length:strlen("<html><body><hr>hi</body></html>")]
  documentAttributes:NULL];

However, the <hr> tag doesn't appear. Adding the string to my view works though, because I can see the hi.

3 Answers 3

13

This is quite an old question, but worth answering more fully I think.

An easy way to get a horizontal rule is to set up a paragraph with a tab-stop at the length of rule you want, with its alignment set to centred. Then set the text to U+00A0 U+0009 U+00A0 and set the strikethrough attribute on the text. The length of the rule is set by the location of the tab stop. This has the significant advantage of working out of the box with copy and paste — and indeed with other programs that use the text system.

Another way that would work, if you wanted (for instance) a horizontal rule that expanded with the text is to use NSTextAttachment with a custom cell. The cell is passed a line fragment rectangle and can set its own frame as required. FWIW, you don’t have to use an image for NSTextAttachment; your cell can do custom drawing. The problem you will have, though, is that copy and paste will set the cell type based on the attachment content (this makes sense because your custom cell isn’t available in other applications). You can take a hybrid approach and set an image as the attachment while doing your own custom rendering in your application; a gold-plated version might use custom tags in a PNG attachment to indicate that it should be custom rendered.

You might also be able to use NSTextTable to draw a rule — I haven’t tried that approach, but I suspect it could be made to work easily enough.

Finally, you could choose to do what Kevin suggests — calculate the appropriate position using NSLayoutManager and render it yourself.

2
  • alastair, I think I'm digging this approach, but do you have a little code that might clarify it? Mar 17, 2015 at 21:18
  • Thanks for your effort... The first solution works great (minor changes needed for my case)
    – Yitzchak
    Sep 11, 2017 at 20:03
2

I've tried to follow the first approach of the @alasair's instructions and I succeeded using this in Swift:

let hr = NSAttributedString(string: "\n\r\u{00A0} \u{0009} \u{00A0}\n\n", attributes: [.strikethroughStyle: NSUnderlineStyle.single.rawValue, .strikethroughColor: UIColor.gray])

This will render the following line, stretching across the whole screen width:

horizontal line

1
  • 1
    I copy/pasted this code and it worked beautifully in dark and light colour schemes.
    – Adam
    May 17, 2021 at 4:00
1

You're going to have to draw it yourself. <hr> is not text, or an attribute that modifies text. Therefore, NSAttributedString cannot represent it and the Cocoa text system or CoreText has no facility to draw it.

5
  • Hmm, if I can't use NSAttributedString, then how could I insert the <hr> into a NSTextView?
    – houbysoft
    Jul 2, 2012 at 23:05
  • @houbysoft: I think you're still going to have to draw it yourself. Put some space in there in your text rendering, calculate the position, and draw it whenever that portion of the text view is visible. Jul 2, 2012 at 23:05
  • There really isn't a better (/easier) way? I know for instance that you can add images to a NSAttributedString via a NSTextAttachment, maybe I could use that then?
    – houbysoft
    Jul 2, 2012 at 23:10
  • @houbysoft: I suppose you could try dynamically creating an image wherein you draw the <hr>, and then attach that, but if the width of your text field ever changes, you're in trouble. Not to mention the fact that you're still drawing the darn thing yourself. Jul 2, 2012 at 23:41
  • right... I might just give up on the <hr> then. It's not essential, so I can't really justify spending too much time on it, but it'd be a nice feature to have.
    – houbysoft
    Jul 3, 2012 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.