ios 将 NSMutableAttributedString 转换为 NSString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19309908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:17:27  来源:igfitidea点击:

Convert NSMutableAttributedString to NSString

iosobjective-cnsstringuilabelnsmutableattributedstring

提问by tech_human

Can we not convert NSMutableAttributedStringto NSString?

我们不能转换NSMutableAttributedStringNSString?

I have two NSMutableAttributedStringsand I am appending the 2nd string onto 1st as below:

我有两个NSMutableAttributedStrings,我将第二个字符串附加到第一个字符串,如下所示:

[string1 appendAttributedString:string2];

[string1 appendAttributedString:string2];

Since I have to display string1 on a label I do:

因为我必须在标签上显示 string1,所以我这样做:

self.label1.text = (NSString *)string1;

I am getting "unrecognized selector sent to instance"error.

我收到"unrecognized selector sent to instance"错误。

Am I doing anything wrong here? Isn't this the correct way to assign a NSMutableAttributedStringto text property of a label?

我在这里做错了什么吗?这不是NSMutableAttributedString为标签的文本属性分配 a 的正确方法吗?

回答by rmaddy

You can't use a cast to convert an object from one type to another. Use the provided method:

您不能使用强制转换将对象从一种类型转换为另一种类型。使用提供的方法:

label1.text = [string1 string];

Better yet, use the attributed string:

更好的是,使用属性字符串:

label1.attributedText = string1

回答by Juraj Antas

NSAttributtedStringincludes a .stringproperty. From there, you can take NSStringwithout attributes.

NSAttributtedString包括.string财产。从那里,你可以NSString不带属性。

So:

所以:

NSAttributtedString* someString;
NSString* string = someString.string;

回答by Mohammad Parvez

NSAttributtedString have a property string and it is read only property you can not change it.

NSAttributtedString 有一个属性字符串,它是只读属性,您不能更改它。

NSAttributtedString* attributtedString;
NSString* plainString = attributtedString.string;

回答by Kampai

Apart from @rmaddy's answer, mine case is different here.

除了@rmaddy 的回答,我的情况在这里有所不同。

Actually I used NSMutableAttributedStringin JSON parsing to send details on server.

实际上我NSMutableAttributedString在 JSON 解析中用于在服务器上发送详细信息。

At parsing time I got exception because NSMutableAttributedStringcontains information about other attributes too, like color space. Because of that it wont parse.

在解析时,我遇到了异常,因为也NSMutableAttributedString包含有关其他属性的信息,例如color space. 因此它不会解析。

I tried many other ways but finally got solution to get string using below code:

我尝试了很多其他方法,但最终得到了使用以下代码获取字符串的解决方案:

// "amountString" is NSMutableAttributedString string object

NSMutableAttributedString *mutableString = (NSMutableAttributedString *)amountString;
amountValueString = [mutableString string];
amountValueString = [NSString stringWithFormat:@"%@", amountString];

NSRange fullRange = NSMakeRange(0, amountString.length);
NSAttributedString *attStr = [mutableString attributedSubstringFromRange:fullRange];
NSDictionary *documentAttributes = @{NSDocumentTypeDocumentAttribute:NSPlainTextDocumentType};

NSData *textData = [attStr dataFromRange:fullRange documentAttributes:documentAttributes error:NULL];
NSString *amountValueString = [[NSString alloc] initWithData:textData encoding:NSUTF8StringEncoding];