NSAttributedString '\\n' ignored

落花浮王杯 提交于 2019-12-04 23:09:39
Blasco73

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!