views:

39

answers:

2

I have created a string that depends on what the user inputs into the textfield:

NSString *book = [[NSString alloc] initWithFormat:@"%@%@", bookTitle.text, afterbook];

Then this string is part of a bigger string:

NSString *msg = [[NSString alloc] initWithFormat:@"%@%@%@%@%@%@%@%@%@%@%@%@%@%@", lastname, 
      firstname, secondfirstname, secondlastname, thirdfirstname, thirdlastname, book, numb, volume, pubcity, pubcomp, year, pages, print];

I want to format the NSString *book so that it is italics. I don't care about displaying it on the view, but I do want to be able to copy this to the clipboard so I can paste it to a program like Pages on the iPad.

A: 

You cannot format an NSString to be italicised.

Source

jrtc27
+4  A: 

NSStrings are plain text. They have no notion of styling, fonts, colors, or anything else that only becomes relevant when drawing the string.

You need to either use UIKit to draw each substring the way you want it, or create an NSAttributedString, use the Core Text string attributes on the appropriate range to apply the italics, and use Core Text to draw the whole attributed string.

Peter Hosey
Thanks for the quick response. Well I knew I would need an attributed string, but I didn't know what to do after that. I do have it displaying in a UIWebView the way I want it to. Is there anyway to copy the rich text from that to my clipboard?
kp2813
No idea. On the Mac, AppKit adds methods to NSAttributedString to convert to and from RTF, HTML, and a few other formats. On iOS, of course, no AppKit, and I can't find any such methods in iOS Foundation or iOS CFAttributedString or UIKit, and UIPasteboard doesn't appear to offer anything in that direction either. That'd be another question anyway, I think.
Peter Hosey
NSAttributedString is in Foundation, not AppKit. It was also added to iOS in 3.2, docs here (although it looks to be the same as in Mac, method-wise): http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/Reference/Reference.html
zadr
zadr: The class is in Foundation. The methods to convert to and from RTF, HTML, and other formats are added by AppKit. http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSAttributedString_AppKitAdditions/
Peter Hosey
You're right. Misread "AppKit adds methods to…" as "AppKit adds…". Apologies. Also, Colloquy offers an addition to NSAttributedString that takes an HTML fragment and gives you a NSAttributedString; http://colloquy.info/project/browser/trunk/Additions/NSAttributedStringAdditions.h for the header. However, you'll need to add a category to UIColor to get the html attribute values. Different can of worms, BSD licensed if you want to open it.
zadr
zadr: I thought Colloquy was GPL?
Peter Hosey
Desktop Colloquy's codebase is partially GPL (tabs from Adium, and older code). The additions, Chat Core and Mobile Colloquy are BSD.
zadr