NSAttributedString '\n' ignored
Asked Answered
E

2

17

I have a single view whose only UI element is a UITextView. In viewDidLoad: I create an attributed string with "Text\n" and set the text view's attributed text as such:

NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"Text\n"];  
[self.textView setAttributedText:string];

My problem is that the line break is ignored when I run the app.

If I use an NSString and setText: that doesn't happen.

NSString *string = [[NSString alloc] initWithString:@"Text\n"];  
[self.textView setText:string];

Can anyone shed some light on what is happening? I can't seem to find anything in documentation or otherwise.

Engrail answered 24/3, 2013 at 2:22 Comment(0)
S
7

You can use this:

 NSMutableAttributedString *cr = [[NSMutableAttributedString alloc] initWithString: @"\n"];

So if you have two NSMutableAttributedString (string1 and string2) you can do:

[string1 appendAttributedString: cr];
[string1 appendAttributedString: string2];

It's horrible and not elegant.... but it works!!!

Sorilda answered 1/7, 2014 at 13:6 Comment(1)
NSAttributedString can not use appendAttributedString because it can not be changed. Is it correct use NSMutableAttributedString?African
V
3

I would advise you to use:

[[NSAttributedString alloc] initWithHTML:[@"Text<BR />" dataUsingEncoding:NSUTF8StringEncoding] documentAttributes:NULL];

I use this all the time and it works perfectly. I even made a macro out of it:

#define html2AttributedString(htmlString)                                                                   \
    [[[NSAttributedString alloc] initWithHTML:[(htmlString) dataUsingEncoding:NSUTF8StringEncoding]         \
    documentAttributes:NULL] autorelease]

And then, you can make macro for color, alignment, font, etc…

In short, you just need to replace all the "\n" in your string with <BR /> and use the code provided above. For the replacement part, use:

[yourString stringByReplacingOccurrencesOfString:... withString:…];

For iOS, you can check out this page in which they give you a replacement.

Our initWithHTML methods aim to perfectly match the output from the Mac version. This is possible to achieve for characters and I have unit tests in place that make certain this keeps being perfect. Regarding the attributes there are many things that have to be done slightly different on iOS to achieve the same look. I won’t bother you with the details there.

Valerlan answered 24/3, 2013 at 2:25 Comment(4)
The ‘initWithHTML:‘ method isn't available in iOS.Erlandson
At this point I'm more concerned with understanding why I'm not getting the expected behavior but I'll keep this in mind if I end up having to work around it.Engrail
@Engrail Then, you can check on the side of: the paragraph configuration of your attributed string, or on the configuration of your UITextView. Check the page 29 and after of this document developer.apple.com/library/mac/documentation/cocoa/conceptual/…Valerlan
Brilliant! This should be the selected answer.Poussette

© 2022 - 2024 — McMap. All rights reserved.