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