1

I have this :

var attributedString = NSMutableAttributedString(string:"Welcome %@ to %@")

And I want to format this to add some other NSMutableAttributedString instead of %@

How can I achieve that?

Thanks

0

3 Answers 3

3

You can replace NSAttributeString with another NSAttributeString by following way. here in this example, you need to fine location of %@ symbol.

        let stringVariable = "Welcome @ location"

        var attributedString = NSMutableAttributedString(string:stringVariable)

        var attributedStringAnother = NSMutableAttributedString(string:"abc")


        attributedString.replaceCharactersInRange(NSRange(location: 8,length: 1), withAttributedString: attributedStringAnother)

        println(attributedString)

Here location: 8 is a location @ symbol.

Output is :

Welcome abc location{
}

Hope this help you.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

var attributedString = NSMutableAttributedString(string:"Welcome %@ to %@")

let result = attributedString.string.stringByReplacingOccurrencesOfString("%@", withString:"TEST")

var newAtrString = NSMutableAttributedString(string: result)

1 Comment

I want to replace this string with other attributed string. Not with NSString
1

In Swift you can do it this way:

var attributedString = NSMutableAttributedString(string:"Welcome \(anotherAttributedString1) to \(anotherAttributedString2)")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.