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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With