Go: "ios Development Daily Small notes (11)" iOS8 update left "pit" nsattributedstring Set underline Nsunderlinestyleattributename property must be NSNumber

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong><em>This article is a piece of my "ios development daily small notes" series, which is recorded in the development work today, can be explained with a short article or a small demo demo small Tips. They may give the user experience, the code efficiency to get some improvement, or have not been in touch with the technology, very happy to learn, put here to be a little bit. The role of 90% is to help you review, remember, review.</em></strong></p></p><p><p></p></p><p><p>The test team's small partners, iOS8 just released, they put the test equipment rapidly upgraded, and then is the face of various bugs and their various schadenfreude laugh. No way, honestly <span id="8_nwp">fix bug!. </span></p></p><p><p>Let's take a look at one of the problems I met today:</p></p><p><p>project, I read a simplified html-formatted string into memory, and then, with the Nshtmltextdocumenttype type option, initialized An instance of the Nsattributedstring type and displayed it in UITEXTVIEW.</p></p><p><p><strong>Originally in the iOS7, the display has no problem, regardless of setting the color of the place, or underline, are completely ok. But after upgrading the iOS8, Uitextview completely did not show. The error in log is also confusing:</strong></p></p><pre>2014-09-25 21:48:36.495 attributedtextios8demo[3163:24438]-[__nscfstring _getvalue:fortype:]: Unrecognized selector Sent to instance 0xae846f02014-09-25 21:48:36.795 attributedtextios8demo[3163:24438] <nsatstypesetter:0xaebd580 : Exception-[__nscfstring _getvalue:fortype:]: Unrecognized selector sent to instance 0xae846f0 raised during typeset Ting<span id="7_nwp">Layout Manager <NSLayoutManager:0xaebc9f0> 1 containers, text backing has characters currently holding 69 Glyphs. Glyph Tree contents:69 characters, glyphs, 1 nodes, node bytes, + storage bytes, 544 Total bytes, 7.88 bytes per character, 7.88 bytes per glyph Layout Tree contents:69 characters, glyphs, 0 laid glyphs, 0 laid line fragments, 1 nodes, node bytes, 0 storage bytes, Total bytes, 0.46 bytes per character, 0.46 bytes per glyph, 0.00 laid glyphs Per laid line fragment, 0.00 bytes per laid line fragment, Glyph range {0 69}. Ignoring ... 2014-09-25 21:48:36.836 attributedtextios8demo[3163:24438]-[__nscfstring _getvalue:fortype:]: Unrecognized selector Sent to instance 0xae846f02014-09-25 21:48:36.837 attributedtextios8demo[3163:24438] <nsatstypesetter:0xaebd580 : Exception-[__nscfstring _getvalue:fortype:]: Unrecognized selector sent to instance 0xae846f0 raised during typeset Ting<span id="6_nwp"><span id="6_nwp">layout Manager <NSLayoutManager:0xaebc9f0> 1 containers, text backing has characters currently holding Glyphs. Glyph Tree contents:69 characters, glyphs, 1 nodes, node bytes, + storage bytes, 544 Total bytes, 7.88 bytes per character, 7.88 bytes per glyph Layout Tree contents:69 characters, glyphs, 0 laid glyphs, 0 laid line fragments, 1 nodes, node bytes, 0 storage bytes, Total bytes, 0.46 bytes per character, 0.46 bytes per glyph, 0.00 laid glyphs Per laid line <span id="5_nwp">fragment, 0.00 bytes per laid line <span id="2_nwp">fragment, Glyph range {0 69}. Ignoring</span> </span> </span> ...</span></span></pre><p><p><strong>I looked at it and thought it was about a __nscfstring object calling a method that does not belong to it _getvalue:fortype: and, unexpectedly, there is no crash! But it's too abstract to know exactly where the problem is. I had to use the killer, block by piece analysis, after half an hour of various Google search (also have a variety of fq= =) and code analysis, finally found that the original problem is set "underline" this Link.</strong></p></p><p><p>First look at my question Code:</p></p><pre>1-(void) viewdidload {2 [super viewdidload]; 3//do Any additional setup after loading the view, typically<span id="1_nwp">From a nib. 4 5 NSString *<span id="4_nwp">data = [[nsbundle mainbundle] pathforresource:@ "111" oftype:@ "plist"];//read file 6 nsmutabledictionary *infodict = [NSMut Abledictionary Dictionarywithcontentsoffile:data];//read The data in the file 7 nsstring *string = [infodict objectforkey:@ "aa"];//removed String Data 8 9 nstextstorage *storage = [[nstextstorage alloc] initwithdata:[string<span id="0_nwp"><span id="0_nwp">datausingencoding:nsutf8stringencoding] options:@{nsdocumenttypedocumentattribute:nshtmltextdocumenttype} Documentattributes:nil error:nil];<br>Initialize the NSTextStorage10 [storage addattribute:nsunderlinestyleattributename value:[nsstring stringwithformat in HTML mode: @ "%d", nsunderlinestylesingle] range:nsmakerange (10, 20)];//set underline all nsdictionary *dict=[nsdictionary Dictionar ywithobjectsandkeys:storage,@ "storage", nil];13 Uitextview *textview = [[uitextview alloc] initwithframe:cgrect Make (0,];15, +) textview.attributedtext = [dict objectforkey:@ "storage"];16 [self.view addsubview : textview];18}</span></span></span></span></pre><p><p><strong>My value is set to <span class="s1">[nsstring<span class="s1"> stringwithformat<span class="s1">:<span class="s2">@ "%d"<span class="s1">, Nsunderlinestylesingle<span class="s1">], because there is absolutely no problem in iOS7, So I always thought it was right! But in fact, in the iOS8, this is wrong! Reference:</span> </span> </span> </span> </span> </span></strong></p></p><p><p>Https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/Articles/standardAttributes.html</p></p><p><p><strong>originally, Nsunderlinestyleattributename should be the NSNumber type, instead of <span class="s1">[nsnumber<span class="s1">numberwithint<span class="s1">: Nsunderlinestylesingle<span class="s1">] Is Correct. </span></span></span></span></strong></p></p><p><p><strong><span class="s1"><span class="s1"><span class="s1"><span class="s1">It is no wonder that the "<strong>_getvalue:fortype:" error occurs because internally when the method is called, the object that accepts the message is a string type, not a number. That makes sense! </strong></span></span></span></span></strong></p></p><p class="para Objective-C"><p class="para Objective-C"><code class="code-voice">NSUnderlineStyleAttributeName</code></p></p><p class="para"><p class="para">The value of this attribute is a <code class="code-voice u-api">NSNumber</code> object containing an integer. This value indicates whether the text was under lined and corresponds to one of the<span id="3_nwp">constants described in <code class="code-voice">“Underline and Strikethrough Style Attributes”</code> . The default value for this attribute is <code class="code-voice">NSUnderlineStyleNone</code> .</span></p></p><p class="para"><p class="para">Why IOS7 can be used in NSSTRING,IOS8 will be the error must use nsnumber? Maybe it was iOS8. to fit the swift strong type, did this change?</p></p><p class="para"><p class="para"></p></p><p class="para"><p class="para">Demo Address: Https://github.com/pigpigdaddy/AttributedTextIOS8Demo</p></p><p><p></p></p><p><p>Go: "ios Development Daily Small notes (11)" iOS8 update left "pit" nsattributedstring Set underline Nsunderlinestyleattributename property must be NSNumber</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.