FAQ Database Discussion Community


Performance of measuring text width in AppKit

objective-c,cocoa,nsattributedstring,nslayoutmanager,nstextstorage
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 instead of height) Here are some performance metrics Count\Mechanism sizeWithAttributes NSAttributedString NSLayoutManager...

How do I make DTCoreText use the System Font?

ios,objective-c,uikit,nsattributedstring,dtcoretext
I want to use DTCoreText in my app to convert HTML to NSAttributedString, but I can't figure out how to use the system font (as of iOS 8: Helvetica Neue) as the base font. By default DTCoreText seems to create the NSAttributedString with the Times New Roman font. I'd really...

NSAttributed string not working in swift

ios,swift,nsattributedstring
I am trying to use attributed string to customize a label but getting weird errors in swift. func redBlackSubstring(substring: String) { self.font = UIFont(name: "HelveticaNeue", size: 12.0) var theRange: Range<String.Index>! = self.text?.rangeOfString(substring) var attributedString = NSMutableAttributedString(string: self.text!) let attribute = [NSForegroundColorAttributeName as NSString: UIColor.blackColor()] attributedString.setAttributes(attribute, range: self.text?.rangeOfString(substring)) self.attributedText = attributedString...

Duplicated UITextView doesn't show all text from the original

ios,objective-c,uitextview,nsattributedstring
I'm trying to make a duplicate UITextView based on another UITextView for sharing purposes. This text view is added as a subview to viewToShare. There is an issue however, the duplicated text view doesn't show all of the original text from the text view I'm copying from. I'm using attributed...

NSForegroundColorAttributeName not working for UILabel

ios,objective-c,nsattributedstring,core-text
I have a problem changing substring color using "NSForegroundColorAttributeName". I'm confused since other attributes for the same substring are being applied(underline). Also tried to use deprecated attribute "UITextAttributeTextColor" and other suggested attribute "kCTForegroundColorAttributeName" and got the same effect. I'm compiling for iOS 7. NSString *freeText = [NSString stringWithFormat:@"(%@)", self.me.activity.text]; int...

Need to get NSRanges of multiple @ symbols and the text that follows them in UILabel's text

ios,objective-c,uilabel,nsattributedstring,nsrange
I have a UILabel that contains text like this: "Hi, my name is John Smith. Here is my twitter handle @johnsmith and I work for this company @somerandomcompany. Thanks for watching!" I need to find the ranges for all of the @ symbols and whatever text comes right after them...

NSAttributedString initWithData and NSHTMLTextDocumentType crash if not on main thread

ios,cocoa,cocoa-touch,nsattributedstring,nsoperationqueue
calling NSAttributedString * as = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; on other than main thread, results in a crash 1 0x194b861fc <redacted> 2 0x19801d31c <redacted> 3 0x198010eb4 _os_once 4 0x19801aa2c pthread_once 5 0x195a0858c <redacted> 6 0x195a07c78 WebKitInitialize 7 0x18bb38918 <redacted> 8...

Confused by NSStringDrawingOptions item meaning

ios,nsstring,nsattributedstring,textkit
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 NSStringDrawingOptions enum. NSStringDrawingUsesLineFragmentOrigin It means specified origin is the line fragment origin, not the base line origin. But what mean of line fragment origin and...

UITextField not displaying NSFontAttributes

html,ios,swift,nsattributedstring
I'm writing in Swift, have an app that includes multiple user-editable textFields and textViews, all with attributedText editing features. Once edited, information is saved to a CoreData object as well as uploaded to a server for backup. The server saves the AttributedStrings as HTML, and it is converted back to...

Change Colour of Small Words ('in') in UITextView using NSMutableAttributedString

ios,objective-c,cocoa-touch,uitextview,nsattributedstring
I'm using NSMutableAttributedString to change the text in a UITextView as the user types. When the user types '#HELLO#' or "#TEST#" or '#test#', those strings should be red (just an example). - (void)textViewDidChange:(UITextView *)textView { NSString *textViewText = textView.text; NSMutableAttributedString * string = [[NSMutableAttributedString alloc]initWithString:textViewText]; NSString *space = @" ";...

RTF to NSAttributedString with Tables (iOS)

ios,rtf,nsattributedstring
I'm using an RTF file as a template in an iOS App. A part of this is a couple tables; however, when doing the conversion from RTF to NSAttributedString, the tables are lost. If I sent them up as plain tabs, the formatting works. However, since most of the text...

How do you clear attributes from NSMutableAttributedString?

ios,objective-c,swift,nsattributedstring
How do you set all the attributes in an NSMutableAttributedString to nothing? Do you have to enumerate through them and remove them? I don't want to create a new one. I am working on the textStorage in NSTextView. Setting a new string resets the cursor position in NSTextView and fires...

NSAttributedString: Fit image to container

html,ios,image,swift,nsattributedstring
I have a NSAttributedString which is made from HTML and it displays some images. The problem is that the images are bigger than the container and I wonder how to fit them in it. Thanks for your help...

replace entire text string in NSAttributedString without modifying other attributes

ios,objective-c,nsattributedstring
I have a reference to NSAttributedString and i want to change the text of the attributed string. I guess i have to created a new NSAttributedString and update the reference with this new string. However when i do this i lose the attributed of previous string. NSAttributedString *newString = [[NSAttributedString...

How to use NSSuperscriptAttributeName for OS X

nsattributedstring,nsmutableattributedstring
I can change the font and size but I'm stuck with making text a superscript. Here is my working code for font and size: aVerseMutableString = NSMutableAttributedString(string: book.verseText, attributes: [NSFontAttributeName:NSFont(name: "Helvetica", size: 18.0)!]) Here is what I'm trying for superscript that's not working: aVerseNumberMutableString = NSMutableAttributedString(string: verseNumber.description, attributes: [NSSuperscriptAttributeName:NSNumber(1)!]) I'm...

Find last visible line index in NSAttributedString's drawWithRect

ios,objective-c,nsattributedstring
I am creating a pdf from user provided text, however I have an issue when the text is too big for the page, I have to calculate where the text would be cut off so I can move the next chunk to the next page. I use this code to...

NSAttributedString from RTF works only with iOS 8, not with iOS 7

ios,uitextview,rtf,nsattributedstring
I'm trying to load an RTF-File in a UITextView with the following Code: NSURL * filePath = [[NSBundle mainBundle] URLForResource:_fileName withExtension:@"rtf"]; NSAttributedString *stringWithRTFAttributes = [[NSAttributedString alloc] initWithFileURL:filePath options:@{NSDocumentTypeDocumentAttribute:NSRTFTextDocumentType} documentAttributes:nil error:nil]; myTextView.attributedText = stringWithRTFAttributes; With iOS 8 it works perfectly, but on iOS 7 it doesn't work, it only displays the...

How to save NSAttributedString to CoreData

ios,swift,core-data,uitextview,nsattributedstring
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: text.attributedText) Data.setValue(a, forKey: "font") Data.managedObjectContext?.save(nil) This is how I back to set:...

how to add specific attributes to a NSAttributedString in Swift

swift,nsattributedstring
This is a simple question but is erroring in several places so I think there's something I'm just not getting (and it's a coworkers app who's no longer here). I am migrating an Objective-C app to Swift and am having some problems with NSAttributedString. There is a note.body which gets...

NSAttributeString battling css scripting already in a string

ios,swift,nsattributedstring
Okay I'm having an issue with NSAttributeString. I'm getting different html/css strings that occupy "busDescriptio" depending on what business you choose from a website database. I'm able to customize the string as I see fit as NSAttributeString but unfortunately on some cases the string already has css scripting in it...

Two different colors in one word in an NSAttributedString

ios,swift,cocoa-touch,nsattributedstring,text-styling
Can I somehow add multiple colors to a single word in an NSAttributedString? In other words, I want to add colors to specific words, with or without a space between colors, such as <red>app</red><blue>ple</blue>. That way apple has two colors. I have attached photos of what happens before and after...

NSAttributedString freeze UITableView

ios,objective-c,uitableview,nsattributedstring
Application really freeze while scrolling with NSAttributedString (When I use NSString it works fine), so there my method: - (void)setSubtitleForCell:(TTTableViewCell *)cell item:(TTPhotoPost *)item { NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData: [item.caption dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; [cell.descriptionLabel setAttributedText:attributedString]; } Any mistakes there? or some way to make...

Underline in attributed text not working in iPhone 6+

ios,iphone,nsattributedstring
I want to create a method that returns me attributed text. here is the code that i am using - (NSAttributedString *)getUnderlineAttributedStringForText:(NSString *)strWholeString andTextToHaveLink:(NSString *)strLink TextColor:(UIColor *)textColor LinkColor:(UIColor *)linkColor withFont:(UIFont*)font { NSRange stringRange = NSMakeRange(0, strWholeString.length); NSRange linkRange = [strWholeString rangeOfString:strLink]; NSLog(@"String Link :: %@",[strWholeString substringWithRange:linkRange]); NSMutableAttributedString *attributedString =...

Add a nsstring at the end of attributed string in textview iOS

ios,objective-c,nsstring,nsattributedstring
I have a text like "Hello There" which will be a attributed string with red color. Then at the end of that attributed string I want to append a nsstring 'Who are you?' But whenever I append a nsstring, the whole string becomes normal nsstring and the property of the...

Change color of specific words in String

ios,swift,nsattributedstring
I'm trying to change the color of specific words which start with a @ however I keep getting an error in the below code. From what I can see it seem to be something with the range variable. I get the following error: cannot invoke 'addAttribute' with an argument list...

How to retrieve photo from UITextView after adding it using NSAttributedString?

ios,objective-c,uitextview,nsattributedstring
I am using the next code to add an image to a UITextView: UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(200,200,140,140)]; textView.font = [UIFont systemFontOfSize:20.0f]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test with emoji"]; NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; textAttachment.image = [UIImage imageNamed:@"Angel.png"]; //for the padding inside the textView textAttachment.image = [UIImage...

lineBreakMode for multiline attributed string

ios,uilabel,nsattributedstring
I have a multiline label that will contain an attributed string. This is the code of the attributed string: NSString * titleString = @"Title"; NSString * informationString = self.myObject.content; NSString * allString = [NSString stringWithFormat:@"%@\n\n%@",titleString,informationString]; NSMutableAttributedString* attributedMessage = [[NSMutableAttributedString alloc]initWithString:allString]; NSMutableParagraphStyle* style=[[NSMutableParagraphStyle alloc]init]; style.alignment = NSTextAlignmentLeft; NSDictionary *attributesTitle =...

How to make a box around a paragraph of NSAttributedString

html,ios,objective-c,nsattributedstring,outline
In HTML you can make a paragraph that has a red outline (or a dotted outline). http://www.w3schools.com/cssref/tryit.asp?filename=trycss_outline I was wondering if something similar can be done using NSAttributedStrings. I am not looking to outline each character, I am looking to place the entire chunk of inside a box and that...

Change UITextView Font w/o Removing Bold/Underline/Italics Formatting

ios,swift,uitextview,nsattributedstring
I have a UITextView in an iOS app, and I want to allow the user to bold/italicize/underline/etc at will, so I gave set the allowsEditingTextAttributes property to true on the text view. However, I would also like to allow the user to change things like font and color. Currently, it...

NSAttributedString: heading on non-paragraphs

ios,swift,nsattributedstring,textkit
I'm building a dictionary with a big UITextView with multiple NSAttributedString for each word. I'm trying to set a fixed space before lines like below but I can't set heading on the text starting by "L'ensemble" as it's not a paragraph (not starting with \n ). Do you have an...

Resize an image that is used in NSAttributedString

ios,objective-c,nsattributedstring
Possible duplicate but the accepted answer below is a simple one-function solution. I'm trying to make a news article details view controller to display the article's image at the top then date then a long description. I learned that i have to use NSAttributedString and only one UITextView in...

Swift - iOS 8 - NSKernAttributeName character spacing problems

ios,string,swift,ios8,nsattributedstring
Part of my application facilitates the drawing of stored string values to a graphics context to be viewed by the user. This is being done with an attributed string with the below Attributes. //adding the attributes to the NSAttributedString let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle let textColor = UIColor.blackColor() let...

Do I use NSAttributedString or UIWebView Swift

ios,swift,uiwebview,uitextview,nsattributedstring
I'm getting different strings that occupy "busDescriptio" depending on what business you choose from a website database. This string contains html tags, images, and links. I was told to use NSAttributedString instead of UIWebView because UIWebView was giving me all sorts of problems such as images in wrong place, text...

Displaying a string with multiple attributes in a label

ios,nsstring,uilabel,nsattributedstring
I need to display a string which will change every second. For example : View will refresh in 10 sec View will refresh in 09 sec View will refresh in 08 sec View will refresh in 07 sec .. View will refresh in 0 sec The above string needs to...

How to Insert NSAttributedString using custom keyboard extension?

ios,nsattributedstring,custom-font,ios-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 NSAttributedString using above approach but I can't see any way to insert NSAttributedString to...

iOS - NSAttributedString tokenized to localization.string element

ios,nsattributedstring,localizable.strings
I have created a html attributed string like this: NSString * htmlString = @"<html><body> Some html string <sup>MD</sup></body></html>"; NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil]; Now i have a localized string that has a token like this: "greeting_label" ="reeveBeets%s \n are awesome"; how...

NSAttributedString alignment not working on html content

ios,objective-c,nsattributedstring
Want to change alignment of html label. Nothing works. I don't see any CSS in the HTML. There are no further settings changing the alignment. I also set left alignment directly on the UILabel. What am I missing? The code is in UILabel extension. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];...

Find CGPoint Location of substring in TextView

ios,swift,sprite-kit,uitextview,nsattributedstring
I'm looking to create a SpriteKit Node positioned at the location of a substring inside a UITextView. How would I retrieve the CGPoint location so I can position the SKNode there? let textFont = [NSFontAttributeName: UIFont(name: "GillSansMT", size: 30.0) ?? UIFont.systemFontOfSize(18.0)] attrString1 = NSMutableAttributedString(string: "My name is Dug.", attributes: textFont)...

How can I vertically center a line in an NSAttributedString with different fonts?

ios,objective-c,nsattributedstring,core-text,nsparagraphstyle
I have a NSAttributedString which is a "table" using NSTextTab stops where the first column is the KEY and the second column is the value Like this: | **KEY 1**|value1| |**KEY TWO**|value2| The font for the KEYS is different size/weight + it is all caps. The rows align so that...

Unable to read contents of .doc file in iOS

ios,nsstring,nsattributedstring
I am trying to read in arabic text that I have contained inside of a .doc file, and use it in my app. Unfortunately, the only way I am able to retrieve the text is if I convert the document into .txt file. Here is the code I have: NSError...

iOS - UILabel attributedText in IB not able to change font size

ios,objective-c,fonts,nsattributedstring
In interfaceBuilder i have created a simple UILabel. It has text inside and i set it to attributedText. I'm not able to change the font size. Each time i try to change the font and run the program the font remains unchanged. In fact what i have tried is after...

HTML to NSAttributedString and NSAttributedString to HTML

html,ios,string,swift,nsattributedstring
i have a problem with NSAttributedString ... I want to convert a HTML string into an NSAttributedString and then work on the NSAttributedString (change some colors, fontsize, fontfamily, background- , foreground-Color) and then convert back plain HTML text from the NSAttributedString. Converting isn't a problem, but on each time I...

UIButton with multi-line titleLabel with NSAttributedString doesn't work in iOS 7 but works in iOS8

ios7,uibutton,nsattributedstring
In my project i have a requirement to show a UIButton that has two lines of text in its titleLabel With NSAttributedString. Its working fine in iOS8 but Where as in iOS7 its not working,i can see two lined text after selected button. This is the code I'm using: calendarBtn=...

Change first line color of UILabel

ios,objective-c,uilabel,nsattributedstring
I'm trying to change a UILabels first line's color. It's not working for some reason. Here is my code: NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: label.attributedText]; [text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[label.text rangeOfString:@"\n"];]; [label setAttributedText: text]; I don't see any changes in the first line....

Change paragraph height (not line spacing) in a UILabel

ios,uilabel,nsattributedstring
Is it possible to limit the distance between paragraphs that were created using \n\n smaller in UILabels using attributed strings? So for example, I would like this: To look like this: Would this involve replace \n\n with something else? Or is there a much simpler solution using NSAttributedString?...

Change text color in specific situation

ios,objective-c,iphone,uitextview,nsattributedstring
I'm using this method to change color to few chars of my UITextView +(void)changeColorToTextObject : (UITextView *)textView ToColor : (UIColor *)color FromLocation : (int)location WithLength : (int)length { NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString: textView.attributedText]; [text addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(location, length)]; [textView setAttributedText: text]; } It works great but after I...