Parsing text from plist to NSAttributedString
Asked Answered
S

2

4

I'm loading in text from a plist and displaying it within my app. Ideally I want to be able to specify more complex formatting such as italics, bold and superscript text, and display it in a custom label such as TTTAttributedLabel.

Are there any available libraries to parse a string in a given format (preferably a simple text format such as Markdown or Textile) into an NSAttributedString? I am aware that solutions are available for parsing RTF and HTML, but this is overkill for my needs - plus I'd like the text to be easily written by hand.

Edit: this is for iOS/UIKit

Scourge answered 24/8, 2012 at 8:30 Comment(1)
These folks apparently are planning to do this: github.com/jverkoey/nimbus/issues/24 Maybe there is already something useful in the repo somewhere.Handicraft
S
1

I've just added an NSString to NSAttributedString lightweight markup parser to MGBoxKit. It's not Markdown or Textile but it's very similar. So far it supports bold, italics, underline, monospacing, and coloured text.

The MGMushParser class could be used standalone, and is fairly easy to extend.

NSString *markup = @"**bold**, //italics//, __underlining__, and `monospacing`, and {#0000FF|coloured text}";

UIFont *baseFont = [UIFont fontWithName:@"HelveticaNeue" size:18];
UIColor *textColor = UIColor.whiteColor;

myLabel.attributedString = [MGMushParser attributedStringFromMush:markup
                               font:baseFont color:textColor];

OHAttributedLabel also has a similar markup parser.

Starch answered 20/11, 2012 at 3:18 Comment(0)
P
3

Caught your edit. For iOS/UIKit there is a project out there called NSAttributedString+HTML that attempts to simulate the functionality available on OS X. On OS X, you would just use some minor HTML to format the string and then parse it into NSAttributedString (or objects, or websites, or files, etc.).

The project I mentioned above attempts to offer the same extensions on iOS. I don't know why, after 6 major releases of iOS, it still lacks such rich tools and pushes all the weight on UIWebView (over WebKit) but that's how it is.

Persistent answered 24/8, 2012 at 9:25 Comment(0)
S
1

I've just added an NSString to NSAttributedString lightweight markup parser to MGBoxKit. It's not Markdown or Textile but it's very similar. So far it supports bold, italics, underline, monospacing, and coloured text.

The MGMushParser class could be used standalone, and is fairly easy to extend.

NSString *markup = @"**bold**, //italics//, __underlining__, and `monospacing`, and {#0000FF|coloured text}";

UIFont *baseFont = [UIFont fontWithName:@"HelveticaNeue" size:18];
UIColor *textColor = UIColor.whiteColor;

myLabel.attributedString = [MGMushParser attributedStringFromMush:markup
                               font:baseFont color:textColor];

OHAttributedLabel also has a similar markup parser.

Starch answered 20/11, 2012 at 3:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.