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
16
votes
0 answers

How do you support Dynamic Type changes in a WKInterfaceLabel?

I have a WKInterfaceLabel in an Apple Watch app running watchOS 5. I have set its attributed string to one that contains a bold font attribute applied to a substring. The boldface shows correctly when the app is run. However, if you change the…
leftspin
  • 2,468
  • 1
  • 25
  • 40
16
votes
2 answers

Color attribute is ignored in NSAttributedString with NSLinkAttributeName

In an NSAttributedString, a range of letters has a link attribute and a custom color attribute. In Xcode 7 with Swift 2, it works: In Xcode 8 with Swift 3, the custom attributed color for the link is always ignored (it should be orange in the…
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
16
votes
4 answers

Are there approaches for using attributed strings in combination with localization?

Suppose there is this string in English and its a requirement to bold or color etc. to a particular part of a string of text: "word1 word2 WORD3 word4" That can be accomplished by setting the attribute for the appropriate range. The range could be…
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
16
votes
2 answers

How to add CSS for HTML to NSAttributedString?

I am trying to load an HTML file via NSAttributedString to display it in a UITextView. So, how can I apply CSS to the text view's content?
Karan Alangat
  • 2,154
  • 4
  • 25
  • 56
15
votes
1 answer

CoreText. How Do I Calculate the Bounding Box of an Attributed String?

In CoreText it is easy ask: "for a given rectangle how much of this attributed string will fit?". CTFrameGetVisibleStringRange(rect).length Will return where in the string the next run of text should begin. My question is: "given an attributed…
dugla
  • 12,774
  • 26
  • 88
  • 136
15
votes
2 answers

NSAttributedString crash when converting HTML to Attrubuted String

I have a situation where try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) crashes the app. In the console it says: Assertion failure in void…
Bocaxica
  • 3,911
  • 3
  • 37
  • 54
15
votes
2 answers

How do you stroke the _outside_ of an NSAttributedString?

I've been using NSStrokeWidthAttributeName on NSAttributedString objects to put an outline around text as it's drawn. The problem is that the stroke is inside the fill area of the text. When the text is small (e.g. 1 pixel thick), the stroking makes…
Tom Dalling
  • 23,305
  • 6
  • 62
  • 80
15
votes
5 answers

Spacing around NSTextAttachment

How do I make spacing around NSTextAttachments like in the example below? In the example No spacing is the default behaviour I get when I append a NSTextAttachment to a NSAttributedString.
15
votes
4 answers

How to trim(remove) white spaces from end of a NSAttributedString

I have a string which has no whitespace at end of the string but when I am converting to NSAttributedString and set to UITextView it was seen some whitespace at end of the UILabel. For making NSAttributedString I am using following code. In my code…
Vvk
  • 4,031
  • 29
  • 51
15
votes
1 answer

Very slow HTML rendering in NSAttributedString

I have UITableView with dynamic sizing cells that displays list of comments in HTML format and I faced with the problem that NSAttributedString renders HTML content extremely slow! Here is snapshot from profiler. I tried to put the…
Madman
  • 3,171
  • 2
  • 32
  • 44
15
votes
1 answer

Confused by NSStringDrawingOptions item meaning

iOS7 and later, we can use - (void)drawWithRect:(CGRect)rect options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context to calculate the string size, but I'm confused with the comments on…
yuyeqingshan
  • 381
  • 3
  • 13
15
votes
5 answers

ios7 font size change when create nsattributedstring from html

I have a UITextView where I'm managing an NSAttributedString, initially entered as normal via the keyboard. I save the attributed string as HTML, which looks fine. When I load it again, and convert it back to an attributed string from the HTML, the…
Marion McKelvie
  • 424
  • 1
  • 5
  • 15
15
votes
1 answer

NSTextView and NSAttributedString

I'm trying to stick an attributed string into my NSTextView, but it's just showing as plain text, no attributes. I created my string like so: NSString *str = @"Parsing Directory Structure\n\n"; NSMutableAttributedString *attrstr =…
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
14
votes
2 answers

How to set NSAttributedString range?

A little confused with using the syntax. I know you are supposed to use NSFontAttributeName, however I don't know how to correctly specify the range. I'm curious about two cases. How do I specify the range for all characters in the text, and how do…
Stefan
  • 908
  • 1
  • 11
  • 33
14
votes
3 answers

NSAttributedString and html styling (bullet alignment)

In my iOS app I'm using NSAttributedString to generate a list of bullets. Unfortunately I'm struggling with making the bullets look presentable. My first attempt was to use regular text and a unicode character for the bullets, basically using a…
Alexander Tsepkov
  • 3,946
  • 3
  • 35
  • 59