Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a NSAttributedString into a NSString using Swift

I have a NSMutableAttributedString and want to convert it back into a simple String.

var attributedString = NSMutableAttributedString(string: "hello w0rld") 

How can I get just the String out of a NSMutableAttributedString the easiest?

like image 931
ixany Avatar asked Aug 25 '14 19:08

ixany


People also ask

What is NSAttributedString in Swift?

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 a string bold in Swift?

let label = UILabel() label. attributedText = NSMutableAttributedString() . bold("Address: ") .

How do you add attributed text in Swift?

Changing an Attributed String First create an NSMutableAttributedString with a new font attribute. You don't use textView. text . Then append another attributed string that doesn't have any attributes set.


2 Answers

Use the string property on NSMutableAttributedString:

var attributedString = NSMutableAttributedString(string: "hello, world!") var s = attributedString.string 
like image 115
mipadi Avatar answered Oct 03 '22 12:10

mipadi


If you strictly want an NSString, not a String use the following:

let s = attributedString.string as NSString 
like image 37
ThomasW Avatar answered Oct 03 '22 12:10

ThomasW