3

I need to make chemistry formulas (SO4^2-), and the easiest way to make subscripts and superscripts seems to be adding UTF-8 characters, since KCTSuperscriptAttributeName: property of NSAttributedString doesn't work.

Is it possible for me to make an nsstring with normal characters and utf-8 characters?

Thanks

Mahir
  • 1,684
  • 5
  • 31
  • 59

3 Answers3

4

Justin's answer is good but I think what you might really be looking for is NSAttributedString (documentation linked for you) or NSMutableAttributedString, where you can add superscripts, subscripts, and other character styles that NSString by itself can't handle.

Take a look at other NSAttributedString questions here or via Google, like this potentially related question or this one.

Hope this helps you out!

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • The second link is actually another one of my questions. I've looked into the NSAttributedString docs but searching through the internet, I've found that people have been saying that the `kSuperscriptAttributeName` property of NSAttributedString doesn't work properly – Mahir Jan 01 '12 at 07:18
  • I've never heard that superscript doesn't work, but I do know that [NSAttributedString is only available in iOS 4.0 & newer](http://stackoverflow.com/questions/5905883/superscripted-ordinal-suffix-in-nsstring). Try it out and let me know if it works for you. – Michael Dautermann Jan 01 '12 at 16:31
  • That answer, though the person answering may not have noticed, uses `NSSupersciptAttributeName` which is meant for Mac OSX, not iOS (http://stackoverflow.com/questions/7121449/nssuperscriptattributename-show-error) – Mahir Jan 01 '12 at 19:24
  • Regardless, I tried with kCTSuperscriptAttributeName in place of NSSuperscriptAttributeName, but still no results – Mahir Jan 01 '12 at 19:25
  • I'm guessing you are setting up your attributed string incorrectly (perhaps in applying the superscript attribute to the wrong range?)? Since I can't see your code, I'm not sure what the correct answer is in your particular case. [Here is a blog post that might help you in adding attributes to specific characters](http://iphonedevelopment.blogspot.com/2011/03/attributed-strings-in-ios.html) in an attributed string. – Michael Dautermann Jan 01 '12 at 19:34
4

According to NSString Reference "NSString is implemented to represent an array of Unicode characters, in other words, a text string."

It would be convenient to write as below: NSString* myStr = @"Any Unicode Character You Want";

Just make sure that your default text encoding is unicode.

Chen-Hai Teng
  • 718
  • 6
  • 16
  • Also, I'm not too familar with utf-8 characters, so I'm not sure whether I read this correctly, but it says [here](http://www.macosxguru.net/article.php?story=20050330194837154) that you shouldn't copy and paste characters directly into the strings. Is that true? – Mahir Jan 01 '12 at 20:47
  • Hi Mahir, about the first question, you can find the setting in Xcode's preferences -- in "Text Editing" tab. The second, the site was written at 2005, and the [NSString](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html) was updated at 2011. Now, you can use utf-8 directly. For example: NSString* utfstr = @"中文"; NSLog(@"utf str %@",utfstr); and the result should also be "utf str 中文". – Chen-Hai Teng Jan 02 '12 at 00:20
3

yes. i assume you know the normal approach to make an NSString - here's one method to create an NSString from a utf8 string: -[NSString initWithUTF8String:].

justin
  • 104,054
  • 14
  • 179
  • 226