Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSAttributedString '\n' ignored

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.

like image 258
winsome Avatar asked Mar 24 '13 02:03

winsome


People also ask

How to remove the extra padding at the end of NSAttributedString?

The first solution is straightforward. Since we know that the extra bottom padding at the end is a new line character, we can write a code to remove it from our NSAttributedString. Here is one way of doing it. 1 Find first non-whitespace character and new line character.

What is NSAttributedString in UIKit?

NSAttributedString is a class in UIKit representing a string with associated attributes such as bold, underscore, or color for portions of its text. Example of NSAttributedString.

What is NSAttributedString in SwiftUI?

NSAttributedString is a class in UIKit representing a string with associated attributes such as bold, underscore, or color for portions of its text. Example of NSAttributedString. In SwiftUI, we don't have an equivalent view that matched the power of NSAttributedString, but we got something similar.

What is nsmutableattributedstring in Java?

The NSAttributedString type represents a string that has a series of attributes applied uniformly. The companion NSMutableAttributedString type can be used to create attributed strings that have overlapping attributes and whose contents can be modified after creation.


2 Answers

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!!!

like image 139
Blasco73 Avatar answered Sep 21 '22 13:09

Blasco73


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.

like image 28
Jean Avatar answered Sep 21 '22 13:09

Jean