nsattributedstring

Replace regex matches in attributed string with image in Objective-C

旧巷老猫 提交于 2019-12-06 11:54:38
问题 My goal is to store the information for an attributed string in Parse.com. I decided to come up with an encoding for attributed text for my images that works by replacing any string {X} in braces with the corresponding image. For example: Picture of 2 colorless mana: {X} Should produce an attributed string where {X} is replaced by an image. This is what I've tried: NSString *formattedText = @"This will cost {2}{PW}{PW} to cast."; NSRegularExpression *regex = [NSRegularExpression

How to save (and retrieve) NSAttributedString in a NSDictionary

感情迁移 提交于 2019-12-06 11:30:42
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: ? 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 conform). If you need something more portable than archived object consider using RTF representation of

How can I set a “hidden” attribute for text inside NSAttributedString?

一世执手 提交于 2019-12-06 11:29:42
问题 I have a Cocoa app with an NSTextView control which holds its text in an NSAttributedString (actually I believe it's a NSMutableAttributedString ). I can easily set and modify different text attributes (such as font, underline, etc.) on different character ranges inside that string. However, I want to set a part of the text as hidden (similar to the effect of the CSS attribute display: none ). When an external event occurs (say a button clicked), I want to unhide or hide that specific range

Adding strikethrough to NSAttributedString in iOS 11 with Swift

拈花ヽ惹草 提交于 2019-12-06 11:02:15
问题 Having some issues getting strikethrough to work. Currently I'm doing the following: theString.addAttributes([ NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue, NSAttributedStringKey.strikethroughColor: UIColor.white ], range: NSMakeRange(0, 1)) It's not showing any sort of strikethrough though. Any ideas? I can't seem to find anything that works. 回答1: I use this to strike through a text in Xcode9/iOS11/Swift4 env let strokeEffect: [NSAttributedStringKey : Any]

NSTextField clicked link colour

烂漫一生 提交于 2019-12-06 10:59:38
I have an NSTextField that contains an NSAttributedString which itself contains a clickable link. I've change the colour of the link for my own styling, however when I click on it it becomes blue and underlined. How can I stop this? Seems that I'm not the only one with this problem, and there's a handy class to solve it: https://github.com/laevandus/NSTextFieldHyperlinks This ( http://developer.apple.com/library/mac/#qa/qa1487/_index.html ) may help you. It will show you how to set/change the blue link color and the underlineAttribute. You may also have a look at the methods

How can initializing NSAttributedString in tableView:heightForRowAtIndexPath: be implicitly recursive?

僤鯓⒐⒋嵵緔 提交于 2019-12-06 07:43:25
问题 I am having a very strange problem when calculating the height of a UITableViewCell . It seems like if I instantiate an NSAttributedString with NSData containing some HTML, a layout cycle is forced on the present view, which ends up calling tableView:heightForRowAtIndexPath: again. And also, heights for all of the other rows are requested in this pass. Luckily, the inner loop of the row height request doesn't have another set of recursive calls within it. Here is the stacktrace: (note frames

Convert array to NSAttributedString

狂风中的少年 提交于 2019-12-06 05:51:55
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. I'd to the old way: NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init]; NSAttributedString *jointElement = [[NSAttributedString alloc] initWithString:@","] for (int i = 0; i

finding range of visible text in UITextView

社会主义新天地 提交于 2019-12-06 04:11:38
问题 I've found code from answers on previous questions that calculate the range of the visible attributed text in a UITextView. It works perfectly, except after I add in newline characters of any type in order to create a new paragraph. In this case, the code gives the incorrect answer, until I manually scroll the view. After scrolling, it gives the correct answer every time. But I need it to give the right answer even without scrolling. Below is my sample project. It is a single view application

UILabel size incorrect for single line of text with lineSpacing and multiple colors

和自甴很熟 提交于 2019-12-06 03:34:28
问题 I'm pretty sure this is actually a UIKit bug but want to get some input to see if I'm missing something silly here. Here is the code I have: // single line with modified line spacing and 2 colors - broken, line spacing is added to the bottom! UILabel *brokenLabel = [[UILabel alloc] init]; brokenLabel.backgroundColor = [UIColor greenColor]; NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Just some text"]; [attributedString addAttributes:@

Difference Between EffectiveRange and LongestEffectiveRange

扶醉桌前 提交于 2019-12-06 03:32:26
问题 In the NSAttributedString class, there are functions that get the values of attributes for certain indices and ranges but I'm unsure of the difference between the selectors attributesAtIndex:effectiveRange and attributesAtIndex:longestEffectiveRange:inRange: and when would I use one instead of the other? Thanks in advance for any clarification 回答1: Let's say you have this kind of attributes: Range [0,2]: Black Background Color Range [0,2]: Bold Font Range [2,4]: Black Background Color Range