"iOS Development Daily Small notes (11)" IOS8 update Left "pit" nsattributedstring set underline Nsunderlinestyleattributename property must be NSNumber

Source: Internet
Author: User

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.

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 fix bug!.

Let's take a look at one of the problems I met today:

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.

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:

 the- the- -  +: -:36.495attributedtextios8demo[3163:24438]-[__nscfstring _getvalue:fortype:]: Unrecognized selector sent to instance0xae846f0 the- the- -  +: -:36.795attributedtextios8demo[3163:24438] <nsatstypesetter:0xaebd580: Exception-[__nscfstring _getvalue:fortype:]: Unrecognized selector sent to instance0xae846f0Raised during typesetting layout manager <nslayoutmanager:0xaebc9f0>1containers, text backing has thecharacters currently holding theglyphs. Glyph Tree Contents: theCharacters, theGlyphs1Nodes +Node bytes, +Storage bytes,544Total Bytes,7.88Bytes per character,7.88bytes per glyph Layout Tree contents: theCharacters, theGlyphs0Laid glyphs,0Laid line fragments,1Nodes +Node bytes,0Storage bytes, +Total Bytes,0.46Bytes per character,0.46Bytes per glyph,0.00Laid glyphs per laid line fragment,0.00bytes per laid line fragment, glyph range {0  the}. Ignoring ... the- the- -  +: -:36.836attributedtextios8demo[3163:24438]-[__nscfstring _getvalue:fortype:]: Unrecognized selector sent to instance0xae846f0 the- the- -  +: -:36.837attributedtextios8demo[3163:24438] <nsatstypesetter:0xaebd580: Exception-[__nscfstring _getvalue:fortype:]: Unrecognized selector sent to instance0xae846f0Raised during typesetting layout manager <nslayoutmanager:0xaebc9f0>1containers, text backing has thecharacters currently holding theglyphs. Glyph Tree Contents: theCharacters, theGlyphs1Nodes +Node bytes, +Storage bytes,544Total Bytes,7.88Bytes per character,7.88bytes per glyph Layout Tree contents: theCharacters, theGlyphs0Laid glyphs,0Laid line fragments,1Nodes +Node bytes,0Storage bytes, +Total Bytes,0.46Bytes per character,0.46Bytes per glyph,0.00Laid glyphs per laid line fragment,0.00bytes per laid line fragment, glyph range {0  the}. Ignoring ...

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.

First look at my question code:

1- (void) Viewdidload {2 [Super Viewdidload];3     //additional setup after loading the view, typically from a nib.4     5NSString *data = [[NSBundle mainbundle] Pathforresource:@"111"OfType:@"plist"];//reading Files6Nsmutabledictionary *infodict =[nsmutabledictionary dictionarywithcontentsoffile:data];//reading data from a file7NSString *string= [Infodict Objectforkey:@"AA"];//fetching String data8     9Nstextstorage *storage = [[Nstextstorage alloc] initwithdata:[stringdatausingencoding:nsutf8stringencoding] Options:@{nsdocumenttypedocumentattribute:nshtmltextdocumenttype} Documentattributes:nil Error:nil];
Initialize the Nstextstorage in HTML modeTen[Storage addattribute:nsunderlinestyleattributename value:[nsstring stringWithFormat:@"%d", Nsunderlinestylesingle] Range:nsmakerange (Ten, -)];//set the underline One ANsdictionary *dict=[nsdictionary Dictionarywithobjectsandkeys:storage,@"Storage", nil]; - -Uitextview *textview = [[Uitextview alloc] Initwithframe:cgrectmake (0, +, the, -)]; theTextview.attributedtext = [Dict objectforkey:@"Storage"]; - - [Self.view Addsubview:textview]; -}

My value is set to [NSString stringwithformat:@ "%d", Nsunderlinestylesingle], Since there is no problem in iOS7, I always thought it was right! But in fact, in the iOS8, this is wrong! Reference:

Https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/AttributedStrings/Articles/standardAttributes.html

Originally, Nsunderlinestyleattributename should be the NSNumber type, instead of [NSNumber numberwithint: Nsunderlinestylesingle] is correct.

It is no wonder that the "_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!

NSUnderlineStyleAttributeName

The value of this attribute is a NSNumber object containing an integer. This value indicates whether the text was underlined and corresponds to one of the constants described in “Underline and Strikethrough Style Attributes” . The default value for this attribute is NSUnderlineStyleNone .

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?

Demo Address: Https://github.com/pigpigdaddy/AttributedTextIOS8Demo

"iOS Development Daily Small notes (11)" IOS8 update Left "pit" nsattributedstring set underline Nsunderlinestyleattributename property must be NSNumber

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.