aryaxt
aryaxt

Reputation: 77596

Objective C - Inserting string in an NSAttributedString?

I have an NSAttributedString, I need to insert some text in the middle of the text in my attributedString, how can i do that?

Upvotes: 8

Views: 5003

Answers (1)

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

NSAttributedString *someAttrString = ...; // the original string you want to modify
NSAttributedString *someOtherAttrString = ...; // the text you want to insert
NSUInteger whereItGoes = ...; // where you want to insert the string

NSMutableAttributedString *mutableString = [someAttrString mutableCopy];
if (mutableString) {
    [mutableString insertAttributedString: someOtherAttrString atIndex: whereItGoes];
    // mutableString now contains the modified data; it's up to you
    // how it gets used in your app.

    [mutableString release];
}

Upvotes: 15

Related Questions