nsattributedstring

How to separate attributes from a NSAttributed String and apply these attributes on other string?

↘锁芯ラ 提交于 2019-12-11 01:02:49
问题 eg. Like we have a NSAttributed string and we need to separate string and attributes, then use these attributes on other string of same length. 回答1: An NSAttributedString may have different attributes for different ranges of the string. To extract these attributes, you can use the enumerateAttributesInRange method. We prepare an array of tuples to hold the results: var extractedAttributes = [(attributes: [String:AnyObject], range: NSRange)]() Each tuple will hold the attributes for a specific

Setting attributedText, NSRangeException error

心不动则不痛 提交于 2019-12-10 22:32:20
问题 Trying to set my UIText view's attributed text properties via selection. Almost works. Action to set text with red font color below. This works sometimes, but often gives an error: Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds' This happens even while there seems to be many more characters in the text view than indicated by the selected range. - (IBAction)setText:(id)sender { NSMutableAttributedString *

How can I save the attributed string (text) into file (swift, cocoa)?

只愿长相守 提交于 2019-12-10 18:56:22
问题 I have NSTextView and I can have text as nsattributedstring. I can save text into .txt file using NSSavePanel, as plain text, but not as formatted text. @IBAction func saveDNA(sender: AnyObject) { let saveDNAtoFile: NSSavePanel = NSSavePanel() saveDNAtoFile.canSelectHiddenExtension = true saveDNAtoFile.runModal() do { let exportedFileURL = saveDNAtoFile.URL let textDNA = self.inputDnaFromUser.string if exportedFileURL != nil { try textDNA!.writeToURL(exportedFileURL!, atomically: false,

How to underline part of string with NSAttributedString objective-c

自闭症网瘾萝莉.ら 提交于 2019-12-10 17:39:58
问题 Underling a string only works when the range starts from zero. If i start it at 1 then it works. The green colour works regardless. + (NSAttributedString*)returnNSAttributedString:(NSString*)string range:(NSRange)range WithColour:(UIColor*)colour WithUnderLine:(BOOL)underline { NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string]; if (underline) { [attributedString addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}

Customize underline pattern in NSAttributedString (iOS7+)

坚强是说给别人听的谎言 提交于 2019-12-10 15:05:39
问题 I'm trying to add a dotted underline style to an NSAttributedString (on iOS7+/TextKit). Of course, I tried the built-in NSUnderlinePatternDot : NSString *string = self.label.text; NSRange underlineRange = [string rangeOfString:@"consetetur sadipscing elitr"]; NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string]; [attString addAttributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle | NSUnderlinePatternDot)} range:underlineRange]; self

How to set both spacing between characters (kern) and strikethrough style for `UILabel`?

放肆的年华 提交于 2019-12-10 14:32:24
问题 I need to set two attributes to a text presented by a UILabel : spacing between letters (kern), and its strikethrough style. Based on the NSAttributedStringKey documentation I have created the following extension to the UILabel : extension UILabel { func setStrikeThroughSpacedText(text: String, kern: CGFloat?) { var attributes: [NSAttributedStringKey : Any] = [:] if let kern = kern { attributes[.kern] = kern } attributes[.strikethroughStyle] = NSNumber(integerLiteral: NSUnderlineStyle

NSAttributeString Height seems align wrong when use both image and text

爱⌒轻易说出口 提交于 2019-12-10 11:48:09
问题 I have a uilabel in tableviewcell ,and the label use NSAttributeString to support both image and text. But I found when the label contains image or text only , the height of label is correct ,but when label contains image and text both ,the height seems not correct .the row height seems little more big than the actual height need and if there is text or images only ,the height is right and I was using my Own subclass from NSTextAttachment to construct image and the attachment code is like

How to add function-call (not hyperlink) to part of an NSAttributedString in a UILabel?

允我心安 提交于 2019-12-10 11:39:53
问题 I need to add some tapGestures to some part of a text, thats in a UILabel. It seems to be able to make a hyperlink - so I guess its possible to make a functioncall as well, but I'm just not sure how to do it. Heres my code: let regularFont = UIFont(name: Constants.robotoRegular, size: 13)! let att1 = [NSFontAttributeName: regularFont] let turqoiseFont = UIFont(name: Constants.robotoBold, size: 13)! let att2 = [NSFontAttributeName: turqoiseFont] let attString1 = NSMutableAttributedString

How to remove padding under last paragraph from NSAttributedString created from HTML using NSHTMLTextDocumentType

元气小坏坏 提交于 2019-12-10 02:16:01
问题 When creating an NSAttributedString from HTML, using NSHTMLTextDocumentType , I'm finding it will add an \n for each paragraph even after the last paragraph. This is adding undesired padding underneath the last paragraph of text that's shown in a UILabel . How does one remove that extra padding for the last paragraph only? NSString *style = @"<style> body { font-family: Avenir; font-size: 18px; color: blue; } p:last-of-type { margin: 0; }</style>"; NSString *html = @"<p>A whole bunch of

NSTextAlignmentJustified not working in iOS7

感情迁移 提交于 2019-12-10 02:11:47
问题 I have an app which uses NSTextAlignmentJustified for an NSAttributedString . In iOS 6 everything is working great. But the same App running in iOS 7 (simulator or device makes no difference) is showing no Justify at all. Also the linespacing seems to have changed dramatically from iOS 6 to 7 . Anyone else encountered this problem? Is there any way to make a justified Textblock in iOS 7 (which works in iOS 6 too?) Regards, Markus 回答1: Ok, i kind of found a way to make the label Justifiy in