InterfaCSS icon indicating copy to clipboard operation
InterfaCSS copied to clipboard

Styling for NSAttributedString

Open TadeasKriz opened this issue 10 years ago • 1 comments

It would be great if there was a way to style an NSAttributedString somehow. I know this might be a bit out of scope of this project, but I would love to be able to style a string I have in code with styles in stylesheet.css.

For example the text might look like:

<span class="bold">Some bold text</span> and some regular. and would result in

Some bold text and some regular.

TadeasKriz avatar Dec 19 '15 16:12 TadeasKriz

There actually is sort of support for this already: you can set attributed string properties like this.

Granted, it's not exactly expressed in the HTMLy way you described, but then again, that's not really what you'd expect in a stylesheet.

But setting attributed string properties directly from a stylesheet is not always what you want of course, and I have been thinking about adding some sort of templating support for attributed string variables. However, it's actually possible to use a form of templating already by doing something like this:

CSS:

@template: "$1" (font: HelveticaNeue-Bold 17) "$2" (font: HelveticaNeue 17)

Code:

NSString* template = [[InterfaCSS sharedInstance] valueOfStyleSheetVariableWithName:@"template"];
template = [template stringByReplacingOccurrencesOfString:@"$1" withString:@"Some bold text"];
template = [template stringByReplacingOccurrencesOfString:@"$2" withString:@" and some regular."];
NSAttributedString* attributedString = [[InterfaCSS sharedInstance].parser transformValue:template asPropertyType:ISSPropertyTypeAttributedString];

tolo avatar Jan 08 '16 19:01 tolo