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
373
votes
31 answers

How do I make an attributed string using Swift?

I am trying to make a simple Coffee Calculator. I need to display the amount of coffee in grams. The "g" symbol for grams needs to be attached to my UILabel that I am using to display the amount. The numbers in the UILabel are changing dynamically…
dcbenji
  • 4,598
  • 5
  • 21
  • 23
317
votes
15 answers

How do you use NSAttributedString?

Multiple colours in an NSString or NSMutableStrings are not possible. So I've heard a little about the NSAttributedString which was introduced with the iPad SDK 3.2 (or around 3.2) and is available on the iPhone as of iPhone SDK 4.0 beta. I would…
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
299
votes
35 answers

Create tap-able "links" in the NSAttributedString of a UILabel?

Many applications have text and in this text are web hyperlinks in rounded rect. When I click them UIWebView opens. What puzzles me is that they often have custom links, for example if words starts with # it is also clickable and the application…
Lope
  • 5,388
  • 4
  • 30
  • 40
261
votes
15 answers

Bold & Non-Bold Text In A Single UILabel?

How would it be possible to include both bold and non-bold text in a uiLabel? I'd rather not use a UIWebView.. I've also read this may be possible using NSAttributedString but I have no idea how to use that. Any ideas? Apple achieves this in several…
DomMaiocchi
  • 2,737
  • 4
  • 17
  • 5
234
votes
23 answers

How can I make a clickable link in an NSAttributedString?

It's trivial to make hyperlinks clickable in a UITextView. You just set the "detect links" checkbox on the view in IB, and it detects HTTP links and turns them into hyperlinks. However, that still means that what the user sees is the "raw" link.…
Duncan C
  • 128,072
  • 22
  • 173
  • 272
186
votes
9 answers

How can I concatenate NSAttributedStrings?

I need to search some strings and set some attributes prior to merging the strings, so having NSStrings -> Concatenate them -> Make NSAttributedString is not an option, is there any way to concatenate attributedString to another attributedString?
user2559108
168
votes
9 answers

Change string color with NSAttributedString?

I have a slider for a survey that display the following strings based on the value of the slider: "Very Bad, Bad, Okay, Good, Very Good". Here is the code for the slider: - (IBAction) sliderValueChanged:(UISlider *)sender { scanLabel.text =…
Adam
  • 1,817
  • 2
  • 11
  • 7
164
votes
18 answers

Convert HTML to NSAttributedString in iOS

I am using a instance of UIWebView to process some text and color it correctly, it gives the result as HTML but rather than displaying it in the UIWebView I want to display it using Core Text with a NSAttributedString. I am able to create and draw…
Joshua
  • 15,200
  • 21
  • 100
  • 172
163
votes
25 answers

boundingRectWithSize for NSAttributedString returning wrong size

I am trying to get the rect for an attributed string, but the boundingRectWithSize call is not respecting the size I pass in and is returning a rect with a single line height as opposed to a large height (it is a long string). I have experimented by…
RunLoop
  • 20,288
  • 21
  • 96
  • 151
157
votes
17 answers

Parsing HTML into NSAttributedText - how to set font?

I am trying to get a snippet of text that is formatted in html to display nicely on an iPhone in a UITableViewCell. So far I have this: NSError* error; NSString* source = @"Nice try, Phil"; NSMutableAttributedString* str =…
phil
  • 4,668
  • 4
  • 33
  • 51
140
votes
16 answers

How to underline a UILabel in swift?

How to underline a UILabel in Swift? I searched the Objective-C ones but couldn't quite get them to work in Swift.
Esqarrouth
  • 38,543
  • 21
  • 161
  • 168
132
votes
9 answers

NSAttributedString add text alignment

How can I add text alignment attribute to an NSAttributedString to center the text? Edit: Am I doing anything wrong? It doesn't seem to change the alignment. CTParagraphStyleSetting setting; setting.spec =…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
119
votes
16 answers

Swift: Display HTML data in a label or textView

I have some HTML data, which contains headings, paragraphs , images and lists tags. Is there a way to display this data in one UITextView or UILabel?
Talha Ahmad Khan
  • 3,416
  • 5
  • 23
  • 38
112
votes
12 answers

Attributed Text Center Alignment

I have tried everything but cannot seem to center this text. Can someone please tell me where the error is. NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new; paragraphStyle.alignment = NSTextAlignmentCenter; label.attributedText…
user3255746
  • 1,377
  • 3
  • 12
  • 13
112
votes
17 answers

Use multiple font colors in a single label

Is there a way to use two, or even three font colors in a single label in iOS? If the text "hello, how are you" were used as an example, the "hello," would be blue, and the "how are you" would be green? Is this possible, it seems easier than…
Justin Rose
  • 1,165
  • 2
  • 10
  • 6
1
2 3
99 100