iOS - UIButton設置文字標題下划線以及下划線顏色


創建button設置可以折行顯示


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(20, 30, 150, 70)];
    [self.view addSubview:button];
    [button setTitle:@"button" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    //button 折行顯示設置
    /*
     NSLineBreakByWordWrapping = 0,     	// Wrap at word boundaries, default
     NSLineBreakByCharWrapping,		// Wrap at character boundaries
     NSLineBreakByClipping,		// Simply clip 裁剪從前面到后面顯示多余的直接裁剪掉
     
         文字過長 button寬度不夠時: 省略號顯示位置...
     NSLineBreakByTruncatingHead,	// Truncate at head of line: "...wxyz" 前面顯示
     NSLineBreakByTruncatingTail,	// Truncate at tail of line: "abcd..." 后面顯示
     NSLineBreakByTruncatingMiddle	// Truncate middle of line:  "ab...yz" 中間顯示省略號
     */
    button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    // you probably want to center it
    button.titleLabel.textAlignment = NSTextAlignmentCenter; // if you want to
    button.layer.borderColor = [UIColor blackColor].CGColor;
    button.layer.borderWidth = 1.0;
    
    // underline Terms and condidtions
    NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:@"View Terms and Conditions"];
    
    //設置下划線...
    /*
     NSUnderlineStyleNone                                    = 0x00, 無下划線
     NSUnderlineStyleSingle                                  = 0x01, 單行下划線
     NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0)      = 0x02, 粗的下划線
     NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0)     = 0x09, 雙下划線
     */
    [tncString addAttribute:NSUnderlineStyleAttributeName
                      value:@(NSUnderlineStyleSingle)
                      range:(NSRange){0,[tncString length]}];
    //此時如果設置字體顏色要這樣
    [tncString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor]  range:NSMakeRange(0,[tncString length])];
    
    //設置下划線顏色...
    [tncString addAttribute:NSUnderlineColorAttributeName value:[UIColor redColor] range:(NSRange){0,[tncString length]}];
    [button setAttributedTitle:tncString forState:UIControlStateNormal];
}

  • 設置button的下划線直接設置文字的屬性NSMutableAttributedStringtncString添加文字屬性NSUnderlineStyleAttributeName並設置下划線樣式,NSUnderlineStyleNone -- 無下划線,NSUnderlineStyleSingle -- 單行下划線,NSUnderlineStyleThick -- 單行加粗下划線,NSUnderlineStyleDouble -- 雙下划線.
  • 設置下划線顏色屬性NSUnderlineColorAttributeName
  • 最主要的是設置button的標題為NSMutableAttributedString包含多種屬性的字符串string.

UILabel下划線設置: http://www.cnblogs.com/adampei-bobo/p/6500782.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM