iOS development-uilabel and UIButton add underline

Source: Internet
Author: User

about Uilabel and UIButton sometimes you need to add underscores, there are generally two ways to pass the default nsmutableattributedstring Settings, the second is to draw an underscore in the drawrect, this article on the simple choice of the first, the second is interested in their own research.

Uilabel Set the underline:

    UILabel *label = [[UILabel alloc] Initwithframe:cgrectmake (Ten, +, 310,)];    Label.backgroundcolor = [Uicolor redcolor];    [Label setlinebreakmode:nslinebreakbywordwrapping];    Label.numberoflines =3;    [Label Setfont:[uifont systemfontofsize:14];    nsmutableattributedstring *content = [[nsmutableattributedstring alloc]initwithstring:[nsstring stringWithFormat:@] Bowen Address: Http://www.cnblogs.com/xiaofeixiang Blog Park-flyelephant QQ Group: 228407086 "];    Nsrange Contentrange = {0,[content length]};    [Content Addattribute:nsunderlinestyleattributename Value:[nsnumber Numberwithinteger:nsunderlinestylesingle] Range:contentrange];        Label.attributedtext = content;    [Self.view Addsubview:label];

UIButton Set the underline:

  UIButton *button=[[uibutton Alloc]initwithframe:cgrectmake (Ten, $, ())];    nsmutableattributedstring *title = [[Nsmutableattributedstring alloc] initwithstring:@ "flyelephant"];    Nsrange Titlerange = {0,[title length]};    [title Addattribute:nsunderlinestyleattributename Value:[nsnumber Numberwithinteger:nsunderlinestylesingle] Range: Titlerange];    [Button Setattributedtitle:title                      forstate:uicontrolstatenormal];    [Button Setbackgroundcolor:[uicolor Redcolor];    [Button.titlelabel Setfont:[uifont systemfontofsize:14];    [Self.view Addsubview:button];

The final effect is as follows:

iOS development-uilabel and UIButton add underline

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.