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
10
votes
5 answers

How to check if UILabel has attributedText or normal text programmatically?

Is there any way to tell if UILabel has its text set using label.attributedText or label.text property? The problem is when you set attributedText, text is also updated and vice versa, so it is not possible to check these properties for nil.
Pavel Alexeev
  • 6,026
  • 4
  • 43
  • 51
10
votes
3 answers

Swift NSAttributedString custom fonts

I've read around for different solutions but nothing seems to work. This code creates a nil exception: [NSFontAttributeName: UIFont(name: "Raleway-SemiBold", size: 16)!] I have the fonts installed properly and they show up correctly in the app…
Razvan Soneriu
  • 587
  • 3
  • 7
  • 23
10
votes
2 answers

How to Insert NSAttributedString using custom keyboard extension?

I want to write text in some custom fonts using keyboard Extension as these apps (1,2,3,4) are doing. I know how we can insert normal string in document proxy. [self.textDocumentProxy insertText:mystring]; I have tried to insert…
jamil
  • 2,419
  • 3
  • 37
  • 64
10
votes
2 answers

Saving custom attributes in NSAttributedString

I need to add a custom attribute to the selected text in an NSTextView. So I can do that by getting the attributed string for the selection, adding a custom attribute to it, and then replacing the selection with my new attributed string. So now I…
regulus6633
  • 18,848
  • 5
  • 41
  • 49
10
votes
1 answer

How to obtain plain 'globe' Unicode character

If you include Unicode characters in an NSString, a lot of them will take on the color set for that text - they're just regular glyphs for that font so they're displayed like any other character. But there are some Unicode characters that are…
Jordan H
  • 52,571
  • 37
  • 201
  • 351
10
votes
2 answers

What is the equivalent of Android's "Spannable" in Objective-C

Im on an iOS app that should able to highlight text, and make it clickable too. I read about NSAttributedString in iOS but it still more complicated than Spannable in android. Is there any other Objective c way to do that, if not; what should i do…
Mutawe
  • 6,464
  • 3
  • 47
  • 90
10
votes
2 answers

NSAttributedString change color at end of string

This must be an easy thing to do, but I cannot figure it out. I have a NSMutableAttributedString that has, for example, "This is a test" in it. I want to color the word "test" blue, which I do with this: NSMutableAttributedString *coloredText =…
Raconteur
  • 1,381
  • 1
  • 15
  • 29
10
votes
1 answer

iOS Line spacing attribute on NSMutableAttributedString

How can we set line spacing or line height on NSMutableAttributedString. I have tried with NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy]; [paragraphStyle setLineSpacing:150]…
Mann
  • 5,477
  • 6
  • 45
  • 57
9
votes
0 answers

HTML string to NSAttributed string, Bullet point with sub bullet point issue Swift

I am converting an HTML string to an NSAttributedstring to set in TextView, while in text, there are bullet points with sub-bullet points. Bullet points are coming correctly but sub-bullet point alignment is changed. My actual HTML Text is this And…
MeetMistry15
  • 231
  • 1
  • 8
9
votes
2 answers

Objective C - Change all attributes in NSAttributedString?

[attributedString enumerateAttributesInRange:range options:NSAttributedStringEnumerationReverse usingBlock: ^(NSDictionary *attributes, NSRange range, BOOL *stop) { NSMutableDictionary *mutableAttributes = [NSMutableDictionary…
aryaxt
  • 76,198
  • 92
  • 293
  • 442
9
votes
1 answer

Saving and pasting an attributed string with a custom NSTextBlock

I am trying to create a custom NSTextBlock, much like the one Apple did at WWDC 18 (23 mins in). Full demo project here. Okay, so it works great when I'm editing and marking a paragraph with my paragraph style that has the text block attached.…
9
votes
2 answers

Emoji loss when using NSMutableAttributedString on UILabel

I have got some strange problem while using NSMutableAttributedString for setting text part on UILabel. It shows some bizarre symbol for specific emojis. Here's the code i use and the screenshot of the problem. guard var _comment = comment.comment…
9
votes
1 answer

Convert attributed text to HTML in Swift 4

I am trying to convert attributed text to HTML in Swift 4 so that it can be stored in Firebase/Firestore and synced across different devices/platforms. I have read every article I can find on Stackoverflow including Convert attributed string, to,…
Steve Robertson
  • 209
  • 5
  • 12
9
votes
1 answer

adjustsFontForContentSizeCategory in UILabel with attributed text

I thought I would save others hours of debugging and say that setting adjustsFontForContentSizeCategory to true has no affect on labels you set the attributedText value of (at least in tableview cell labels). Luckily the solution is to set the font…
GilroyKilroy
  • 847
  • 10
  • 17
9
votes
2 answers

Large Navigation Bar Text With Multiple Colors

iOS 11 introduces the option for larger text in the Navigation Bar. I would like to have a title that uses multiple colors. For example: It's fairly easy to set the title, and even to change the color of the entire title: [[self navigationItem]…
Axeva
  • 4,697
  • 6
  • 40
  • 64