Questions tagged [nsattributedstring]

NSAttributedString objects manage character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string.

NSAttributedString and NSMutableAttributedString declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively. The Foundation framework defines only the basic functionality for attributed strings; in Mac OS X, additional methods supporting RTF, graphics attributes, and drawing attributed strings are described in NSAttributedString Additions, found in the Application Kit. The Application Kit also uses a subclass of NSMutableAttributedString, called NSTextStorage, to provide the storage for the Application Kit’s extended text-handling system.

In Mac OS X, the Application Kit also uses NSParagraphStyle and its subclass NSMutableParagraphStyle to encapsulate the paragraph or ruler attributes used by the NSAttributedString classes.

An attributed string identifies attributes by name, storing a value under the name in an NSDictionary object. In Mac OS X, standard attribute keys are described in the “Constants” section of NSAttributedString Application Kit Additions Reference. You can also assign any attribute name/value pair you wish to a range of characters—it is up to your application to interpret custom attributes (see Attributed String Programming Guide). If you are using attributed strings with the Core Text framework, you can also use the attribute keys defined by that framework.

Note that the default font for NSAttributedString objects is Helvetica 12-point, which differs from the Mac OS X system font Lucida Grande, so you may wish to create the string with non-default attributes suitable for your application using, for example, initWithString:attributes:.

Resource

2249 questions
26
votes
5 answers

How to make subscripts and superscripts using NSAttributedString?

I need to make subscripts for chemistry formulas (H2O, Na^2+, etc)? Is this possible to do with NSAttributedString, or is there an alternative/easier way to make subscripts?
Mahir
  • 1,684
  • 5
  • 31
  • 59
26
votes
8 answers

UITextView linkTextAttributes font attribute not applied to NSAttributedString

I have an NSAttributedString generated from HTML which includes some links. The attributed string is shown in a UITextView. I wish to apply a different font style for the links and am setting linkTextAttributes for this. I've added…
lostInTransit
  • 70,519
  • 61
  • 198
  • 274
26
votes
3 answers

NSAttributedString initWithHTML incorrect character encoding?

-[NSMutableAttributedString initWithHTML:documentAttributes:] seems to mangle special characters: NSString *html = @"“Hello” World"; // notice the smart quotes NSData *htmlData = [html…
alltom
  • 3,162
  • 4
  • 31
  • 47
25
votes
2 answers

Setting NSUnderlineStyle causes unrecogognized selector exception

I am trying to underline a string with NSAttributed string. For some reason, my lines cause the exception: -[_SwiftValue _getValue:forType:]: unrecognized selector sent to instance The result is supposed to be used for a UILabel within a…
ff10
  • 3,046
  • 1
  • 32
  • 55
25
votes
19 answers

Add "...Read More" to the end of UILabel

I have a UILabel and in some cases the text is longer then the UILabel itself, so I see the text as "bla bla bla..." I want to add a ...Read More button text at the end of the UILabel.. I've read some posts but they offer solutions that are not good…
ytpm
  • 4,962
  • 6
  • 56
  • 113
25
votes
3 answers

How to add spacing to lines in NSAttributedString

I am making an app that formats screenplays, I am using a NSAttributedString to format the text entered into a UITextView, but some of the lines are too close together. I was wondering if anyone could provide a code example or a tip on how to alter…
James Campbell
  • 3,511
  • 4
  • 33
  • 50
25
votes
13 answers

Calculate Font Size to Fit Frame - Core Text - NSAttributedString - iOS

I have some text which I am drawing into a fixed frame via an NSAttributedString (code below). At the moment I am hard coding the text size to 16. My question is, is there a way to calculate the best fit size for the text for the given frame ? -…
GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113
25
votes
6 answers

NSParagraphStyle line spacing ignored

A simple test that is failed: Make a new project with just one subview (UITextView) and put the following in: - (void)viewDidLoad { [super viewDidLoad]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; …
borrrden
  • 33,256
  • 8
  • 74
  • 109
24
votes
7 answers

Storing NSAttributedString Core Data

I am trying to store an NSAttributedString to a Core Data SQL store. I have the property set as a "transformable", it is optional and it is NOT transient or indexed and the value transformer name is set to default "NSKeyedUnarchiveFromData". In the…
jodm
  • 2,607
  • 3
  • 25
  • 40
24
votes
3 answers

iOS NSAttributedString to HTML

I have an NSAttributed string (coming from HTML) that I set for a UITextView. - (void)setHtml:(NSString *)html { NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding]; // Create the HTML string NSDictionary *importParams =…
Nic Hubbard
  • 41,587
  • 63
  • 251
  • 412
24
votes
4 answers

Top-aligning text of different sizes within a UILabel

How to top-align text of different sizes within a UILabel? An example is top-aligning smaller-sized cent amount with larger-sized dollar amount in price banners. UILabel in iOS6 supports NSAttributedStringwhich allows me to have text of different…
ejel
  • 4,135
  • 9
  • 32
  • 39
24
votes
2 answers

Drawing colored text in UIView's -drawRect: method

I am trying to draw colored text in my UIView subclass. Right now I am using the Single View app template (for testing). There are no modifications except the drawRect: method. The text is drawn but it is always black no matter what I set the color…
RobertJoseph
  • 7,968
  • 12
  • 68
  • 113
23
votes
1 answer

In a UILabel, is it possible to force a line NOT to break in a certain place

I have a UILabel that is supposed to be two lines long. The text is localized into French and English. I'm setting the attributedText property of the label. The text is constructed from three appended strings, say textFragment1, textFragment2 and…
Darren
  • 10,091
  • 18
  • 65
  • 108
23
votes
5 answers

Really close lines with NSAttributedString?

I want to have two lines of text appear really close together (small line spacing) for a button. I have the following code: NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"50 WPM"]; NSMutableParagraphStyle…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
22
votes
1 answer

NSAttributedString and emojis: issue with positions and lengths

I'm coloring some parts of a text coming from an API (think "@mention" as on Twitter) using NSAttributedString. The API gives me the text and an array of entities representing the parts of the text that are mentions (or links, tags, etc) which…
Eric Aya
  • 69,473
  • 35
  • 181
  • 253