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

Preventing line breaks in part of an NSAttributedString

I am working on a UILabel which features large main text followed by smaller text that tells you who said it: Right now it is basically an NSAttributedString with a font attribute on the small text. I would like to set things up so the big text…
Brian Nickel
  • 26,890
  • 5
  • 80
  • 110
18
votes
3 answers

NSAttributedString highlight/background color shows between lines (ugly)

I'm trying to nicely display paragraphs of highlighted in a NSTextView. Right now, I'm doing this by creating a NSAttributedString with a background color. Here's some simplified code: NSDictionary *attributes =…
stevel
  • 1,612
  • 1
  • 14
  • 13
18
votes
4 answers

Localizing attributed UITextView from storyboard

I am using the storyboard and have a view where I have subclassed some UITextViews. My problem is that I am using ibtool --generate-strings-file to extract strings from the storyboard for localization and afterwards use ibtool -write on another…
CracyD
  • 368
  • 4
  • 16
17
votes
3 answers

Changing Background color on NSAttributedString

I just wanted to know how could i change the background color of an NSAttributedString, i can see the background being white all the time, but i want to make it black. Is that possible?? Many thanks!
albertosh
  • 2,416
  • 7
  • 25
  • 32
17
votes
6 answers

How can I accurately detect if a link is clicked inside UILabels in Swift 4?

I have managed to style the links without problem using NSMutableAttributedString but I am unable to accurately detect which character has been clicked. I have tried all the solutions in this question (that I could convert to Swift 4 code) but with…
Dan Bray
  • 7,242
  • 3
  • 52
  • 70
17
votes
4 answers

Cannot convert value of type 'Range' (aka 'Range') to expected argument type 'NSRange' (aka '_NSRange')

I am trying to replace a substring with attributed String. Following is my code. let searchText = self.searchBar.text! let name = item.firstName ?? "" let idNo = "Employee Id. \(item.employeeId ?? "NA")" if let range = name.range(of: searchText,…
Bharath Reddy
  • 636
  • 1
  • 5
  • 21
17
votes
8 answers

Swift 3 NSAttributedString multiple attributes

just started swift 3 and I have problems with swift syntax. i'm trying to display a simple NSAttributedString. so 1st I set my attributes : let attributeFontSaySomething : [String : AnyObject] = [NSFontAttributeName :…
user2206906
  • 1,310
  • 2
  • 13
  • 18
17
votes
11 answers

UITextView with custom font not working when text set to "attributed"

I have a UITextView with some text that came from a .rtf (pasted directly onto Xcode) The context contain only one custom font (Futura Book BT 11.0) If I set the "text(attributed)" property to "plain"= The custom font appear properly from the…
Franck
  • 8,939
  • 8
  • 39
  • 57
17
votes
3 answers

How to Get NSAttributedString from CGSIZE for UITextView?

I am creating a reader app using UITextView and NSAttributedString, I need to split whole attributedString into smaller amount of attributed string to enable Pages Concept. I have method for calculating frame size of given attributed string. …
Manimaran
  • 465
  • 2
  • 10
17
votes
3 answers

UILabel appearance font and attributed string font

In my app I have a global custom font applied to all labels like so: UIFont *font = [UIFont fontWithName:kMyFontName size:15.0]; [[UILabel appearance] setFont:font]; This works fine. However, in some cases I want to be able to specify a different…
nebs
  • 4,939
  • 9
  • 41
  • 70
17
votes
1 answer

UILabel + AutoLayout = error in baseline alignment

I'm having problem while aligning two labels. Two examples to illustrate the problem Example 1 (OK) [leftLabel setText:@"03"]; [rightLabel setText:@"Description3"]; Example 2 (NOK) [leftLabel setText:@"03"]; [rightLabel…
emenegro
  • 6,901
  • 10
  • 45
  • 68
17
votes
3 answers

How to make NSStringDrawingContext shrink text?

I'm trying to use the attributed string API of iOS 6 to calculate the size of text and shrink the font size if necessary. However, I can't get it to work as the documentation says. NSString *string = @"This is a long text that doesn't shrink as it…
Guillaume
  • 4,331
  • 2
  • 28
  • 31
17
votes
4 answers

Replace UITextViews text with attributed string

I have a UITextView and when the user is entering text into it, I want to format the text on the fly. Something like syntax highlighting... For that I'd like to use UITextView... Everything works fine expect one problem: I take the text from the…
Georg
  • 3,664
  • 3
  • 34
  • 75
17
votes
2 answers

NSAttributedString '\n' ignored

I have a single view whose only UI element is a UITextView. In viewDidLoad: I create an attributed string with "Text\n" and set the text view's attributed text as such: NSAttributedString *string = [[NSAttributedString alloc]…
winsome
  • 173
  • 1
  • 6
17
votes
7 answers

Markdown to NSAttributedString library?

Now that NSAttributedString is fully supported in iOS 6, is there a library that will take an NSString with markdown, and convert it to NSAttributedString?
Jason
  • 14,517
  • 25
  • 92
  • 153