Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSMutableAttributedString to NSString

Can we not convert NSMutableAttributedString to NSString?

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

[string1 appendAttributedString:string2];

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

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

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

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

like image 687
tech_human Avatar asked Oct 11 '13 03:10

tech_human


People also ask

What is Nsmutableattributedstring?

A mutable string with associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.

What is an NSAttributedString?

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string.

How do you make an attributed string in Swift?

Creating an Attributed String from MarkdownUse Markdown syntax to initialize an attributed string with text and attributes. Use a Markdown-syntax string to iniitalize an attributed string with standard or custom attributes.


2 Answers

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 
like image 71
rmaddy Avatar answered Oct 04 '22 16:10

rmaddy


NSAttributtedString includes a .string property. From there, you can take NSString without attributes.

So:

NSAttributtedString* someString; NSString* string = someString.string; 
like image 25
Juraj Antas Avatar answered Oct 04 '22 16:10

Juraj Antas