nsattributedstring

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

烈酒焚心 提交于 2019-12-08 03:54:35
问题 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 seems that changing even the color property of the UITextView resets the formatting of the UITextView (i.e. gets rid of the bolding/italics/etc). Is there any way to change this without resetting the formatting?

How to save (and retrieve) NSAttributedString in a NSDictionary

岁酱吖の 提交于 2019-12-08 02:52:13
问题 I am trying to save and retrieve XML data to a file. For this purpose I am using NSDictionary 's writeToFile: The question is, how to write and retrieve an attributed string to and from a file on disk using NSDictionary 's writeToFile: ? 回答1: NSAttributedString is not a property list object, so writeToFile: won't work. It does, however, conform to NSCoding , so you can write it to an archive with your dictionary as the root object (assuming that all the other objects in the dictionary also

Apply bolding to portion of NSAttributedString, while preserving font sizes and faces

 ̄綄美尐妖づ 提交于 2019-12-08 01:09:03
问题 I'm working on a rich text editor for iOS, using a UITextView along with an NSAttributedString . It'll function similarly to traditional ones (i.e. selecting a region, clicking a button, and it applies that effect to that region, while preserving any other attributes on the text. Unfortunately with NSAttributedString , not all of them can be adjusted independently. Several of them (at least bold, font face, and font size) all require passing a UIFont , which'll set all of those attributes for

Convert array to NSAttributedString

孤者浪人 提交于 2019-12-07 21:38:42
问题 I have a NSMutableArray composed of NSAttributedString 's. I'm trying to convert it to a single NSAttributedString separating all the NSAttributedString 's with a character. This approach is similar to the conversion of an array to a NSString with the componentsJoinedByString:@"," method but unfortunately this method doesn't exist with the NSAttributedString . How can I convert the array? Thanks. 回答1: I'd to the old way: NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]

TTTAttributedLabel “Read More >” tail truncation with several attributes possible?

旧时模样 提交于 2019-12-07 18:09:01
问题 TTTAttributedLabel support a custom truncation string via truncationTokenString as well as truncationTokenStringAttributes . However, I am wanting to go a bit further and set several string attributes on the truncation string, including different fonts and colours. This is what I am trying to achieve: The arrow at the end can be achieved using a font icon, so I was thinking of the following string: @"… Read More >" 'HORIZONTAL ELLIPSIS' (U+2026) + Read More + > character from a font .

Is there any way to convert NSAttributedString to HTML in swift?

泄露秘密 提交于 2019-12-07 17:20:25
问题 I want to convert a NSAttributedString to HTML string. I've been searching how to solve it, but I have no results yet. Any idea how can I do it? 回答1: let attrStr = NSAttributedString(string: "Hello World") let documentAttributes = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType] do { let htmlData = try attrStr.dataFromRange(NSMakeRange(0, attrStr.length), documentAttributes:documentAttributes) if let htmlString = String(data:htmlData, encoding:NSUTF8StringEncoding) { print(htmlString

Vertically aligning NSTextAttachment in NSMutableAttributedString

随声附和 提交于 2019-12-07 09:53:31
I'm adding an icon to a UILabel using NSTextAttachment inside an NSMutableAttributedString like this: //Setting up icon let moneyIcon = NSTextAttachment() moneyIcon.image = UIImage(named: "MoneyIcon") let moneyIconString = NSAttributedString(attachment: moneyIcon) //Setting up text let balanceString = NSMutableAttributedString(string: " 1,702,200") balanceString.insert(moneyIconString, at: 0) //Adding string to label self.attributedText = balanceString self.sizeToFit() But for some reason the icon isn't vertically aligned Does anybody know how can I align it? Thank you! This answer , which is

UITextView lineHeightMultiple Clips Top, first line, of Text

陌路散爱 提交于 2019-12-07 09:29:51
问题 In iOS 8, I have a vanilla UITextView that clips the top of the 1st line when a lineHeightMultiple is applied to it's NSMutableParagraphStyle , see image below: It appears as though lineHeightMultiple affects the 1st line of text in addition to subsequent lines. Setting clipsToBounds = false on the UITextView will at least enable the clipped part to show, but you can see from the image below that now the top part of the text is obviously above it's frame: I can fix this by just setting the

NSAttributedString default color/font etc

风格不统一 提交于 2019-12-07 08:34:34
问题 I'm using drawGlyphsForGlyphRange to render an NSAttributedString (in NSView drawRect ) Is there any way to default the color/font etc of an NSAttributedString ? Clearly I could set those values specifically for a given range - but that will then override any specific settings in the string. Got the feeling I have a fundamental misunderstanding of how NSAttributedStrings should be used!! Thanks 回答1: The default font for NSAttributedString objects is Helvetica 12-point on iOS and Lucida Grande

Using attributedText (NSAttributedString) on a UITextView on iOS6 only works with paragraph style or font but not both

房东的猫 提交于 2019-12-07 08:02:44
问题 I have been trying to use the attributedText property of UITextView to add adjustable line height to my custom UITextView. My code works fine in the simulator, but I am unable to make it work on an iPhone5. If I remove the font line the line height works but it the text reverts to a default smaller font. If I add the font, the font works but the paragraph styling is ignored. I have tried the code on a vanilla UITextView in a fresh app with the same behavior, which makes me think this is an