IOS label adds strikethrough (strikethrough) encounters the pit

Source: Internet
Author: User

1. Issues encountered with adding a strikethrough method

-(void) Lastlabeldeal: (NSString *) str1 string: (NSString *) str2 Label: (UILabel *) label{

Nsmutableattributedstring

*STR = [[Nsmutableattributedstring alloc] initwithstring:[nsstring stringwithformat:@ "%@%@", STR1,STR2]];

[Str addattribute:nsstrikethroughstyleattributename value:@ (nsunderlinestylesingle| Nsunderlinepatternsolid) Range:nsmakerange (2, str1.length-2)];

[Str

Addattribute:nsforegroundcolorattributename Value:[uicolor Redcolor] Range:nsmakerange (str1.length + 2,str2.length) ];

The following 3 lines of code are for the label part of the content of the strikethrough (this is used before iOS10.3, no problem, but after 10.3 this implementation will be ineffective)

[Str addattribute:nsstrikethroughstyleattributename value:@ (nsunderlinepatternsolid| Nsunderlinestylesingle) Range:nsmakerange (2, str1.length-2)];

[Str addattribute:nsfontattributename value:[uifont Systemfontofsize:12] Range:nsmakerange (2, str1.length-2)];

[Str addattribute:nsforegroundcolorattributename value:[uicolor Redcolor] Range:nsmakerange (2, str1.length-2)];

Label.attributedtext

= str;

}

2. Final Solution (the above 3 lines of code merge together and eventually achieve the effect)

-(void) Lastlabeldeal: (NSString *) str1 string: (NSString *) str2 Label: (UILabel *) label{

Nsmutableattributedstring

*STR = [[Nsmutableattributedstring alloc] initwithstring:[nsstring stringwithformat:@ "%@%@", STR1,STR2]];

[Str addattribute:nsstrikethroughstyleattributename value:@ (nsunderlinestylesingle| Nsunderlinepatternsolid) Range:nsmakerange (2, str1.length-2)];

[Str

Addattribute:nsforegroundcolorattributename Value:[uicolor Redcolor] Range:nsmakerange (str1.length + 2,str2.length) ];

The above 3 lines of code merge together and eventually achieve the effect

[Str addattributes:@{

nsstrikethroughstyleattributename:@ (Nsunderlinestylethick),

Nsforegroundcolorattributename:

[Uicolor Lightgraycolor],

Nsbaselineoffsetattributename:

@ (0),

Nsfontattributename: [Uifont Systemfontofsize:12]

} range:nsmakerange (2, str1.length-2)];

Label.attributedtext

= str;

}

IOS label adds strikethrough (strikethrough) encounters the pit

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.