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
12
votes
3 answers

NSTextAlignmentJustified not working in iOS7

I have an app which uses NSTextAlignmentJustified for an NSAttributedString. In iOS 6 everything is working great. But the same App running in iOS 7 (simulator or device makes no difference) is showing no Justify at all. Also the linespacing seems…
markusNetural
  • 391
  • 3
  • 6
12
votes
3 answers

How to calculate the height of NSAttributedString, given width and number of lines?

I want to display 3 lines of NSAttributedString. Is there a way to figure out the needed height, based on width and number of lines? And I don't want to create a UILabel to do the size calculation, since I want the calculation to be done in…
lichen19853
  • 1,410
  • 2
  • 14
  • 21
12
votes
4 answers

UILabel attributedText with multiple line break modes

I have a requirement of showing a UILabel with text that has two different styles (different colours, parts of the text bolded). This is solved easily enough by using the attributedText-property. My problem is that the text may or may not be longer…
Ville Rinne
  • 801
  • 1
  • 7
  • 15
12
votes
5 answers

Copy NSAttributedString in UIPasteBoard

How do you copy an NSAttributedString in the pasteboard, to allow the user to paste, or to paste programmatically (with - (void)paste:(id)sender, from UIResponderStandardEditActions protocol). I tried: UIPasteboard *pasteBoard = [UIPasteboard…
Guillaume
  • 21,685
  • 6
  • 63
  • 95
11
votes
1 answer

how to change characters case to Upper in NSAttributedString

I want to convert NSAttributedString containing RTFD to uppercase without losing attributes of existing characters and graphics. Thanks,
AmitSri
  • 1,209
  • 1
  • 20
  • 48
11
votes
2 answers

HTML from NSAttributedString

Rather than converting HTML to an attributed string, I need to convert it back to HTML. This can easily be done on Mac as can be seen here: http://www.justria.com/2011/01/18/how-to-convert-nsattributedstring-to-html-markup/ Unfortuately, the method…
Joshua
  • 15,200
  • 21
  • 100
  • 172
11
votes
2 answers

NSBackgroundColorAttributeName in textView doesn't respect range if highlighted word is first of a line

I'm implementing a "search in chat" feature and I want the searched word to be highlighted in messages. The problem, as described in title, is that if the word is the first of a line (long messages are clearly multi-line), the entire line is…
11
votes
3 answers

Font change on selecting NSTextField

I am wanting to create a simple label with an attributed string. I do this like this: NSDictionary *noNotificationsAttrs = [NSDictionary dictionaryWithObjectsAndKeys:centredStyle, NSParagraphStyleAttributeName, …
maxisme
  • 3,974
  • 9
  • 47
  • 97
11
votes
1 answer

NSAttributed string from html without hanging UI thread

I am trying to load some html text into an NSAttributedString and I am seeing the UI hang. Looking around I am not sure if that is possible as its looks like it may have to run on the main thread: NSAttributedString from HTML in the background…
lostintranslation
  • 23,756
  • 50
  • 159
  • 262
11
votes
8 answers

How can I fake superscript and subscript with Core Text and an Attributed String?

I'm using an NSMutableAttribtuedString in order to build a string with formatting, which I then pass to Core Text to render into a frame. The problem is, that I need to use superscript and subscript. Unless these characters are available in the font…
jowie
  • 8,028
  • 8
  • 55
  • 94
11
votes
2 answers

How to remove padding under last paragraph from NSAttributedString created from HTML using NSHTMLTextDocumentType

When creating an NSAttributedString from HTML, using NSHTMLTextDocumentType, I'm finding it will add an \n for each paragraph even after the last paragraph. This is adding undesired padding underneath the last paragraph of text that's shown in a…
Jordan H
  • 52,571
  • 37
  • 201
  • 351
11
votes
4 answers

How do I display a NSAttributedString on an iPhone?

I believe I understand how to set NSAttributedString values but how on earth do you actually display them in the interface? examples: UILabel, UITextView, etc. Specific instructions would be greatly appreciated/ Thanks!
DomMaiocchi
  • 2,737
  • 4
  • 17
  • 5
11
votes
2 answers

Using Range with NSRange in the NSAttributedString API

I'm attempting to determine the indexes of occurrences of a given string in a String, then generate an NSRange using those indexes in order to add attributes to an NSMutableAttributedString. The problem is rangeOfString returns Range but…
Jordan H
  • 52,571
  • 37
  • 201
  • 351
11
votes
1 answer

Performance of measuring text width in AppKit

Is there a way in AppKit to measure the width of a large number of NSString objects(say a million) really fast? I have tried 3 different ways to do this: [NSString sizeWithAttributes:] [NSAttributedString size] NSLayoutManager (get text width…
11
votes
3 answers

How to save NSAttributedString to CoreData

I want to save the attributedString of my UItextview'text. I tried to convert to string and save. But when I back to set the text'attributedString,it not work. This is how I convert to String: var a = String(stringInterpolationSegment:…
li fubing
  • 113
  • 1
  • 4