2014-06-30 79 views
2

我已經通過這個代碼builed一個按鈕,兩個標題行:UIButton的標題行間距

rootBntUI.titleLabel.font = [UIFont fontWithName:@"Avenir-Black" size:UserListFontSize]; 
[rootBntUI.layer setBorderWidth:0]; 
rootBntUI.titleLabel.textColor = [UIColor whiteColor]; 
rootBntUI.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail; 
rootBntUI.titleLabel.textAlignment = NSTextAlignmentCenter; 
rootBntUI.titleLabel.numberOfLines = 2; 

一切工作正常,但我怎麼能控制按鈕標題的行間距?

回答

2

我已經解決了我的問題,併爲有類似問題的任何人解決了這個問題。

 NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; 
     [style setAlignment:NSTextAlignmentCenter]; 
     [style setLineBreakMode:NSLineBreakByWordWrapping]; 
     [style setLineSpacing:-50]; 

     UIFont *font1 = [UIFont fontWithName:@"Avenir-Black" size:UserListFontSize]; 

     NSDictionary *dict1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle), 
           NSFontAttributeName:font1, 
           NSParagraphStyleAttributeName:style}; 


     NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init]; 
     [attString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", obj] attributes:dict1]]; 
     [FriendBnt setAttributedTitle:attString forState:UIControlStateNormal]; 
     [[FriendBnt titleLabel] setNumberOfLines:0]; 
     [[FriendBnt titleLabel] setLineBreakMode:NSLineBreakByWordWrapping]; 

快樂編碼。

4

你可以從xib做樣式。使用按鈕標題歸因於屬性檢查器,您可以設置所有樣式參數以及間距。

+0

感謝您的快速回復,但我會解決我的問題!再次感謝。 – vietnguyen09

0

這可以在Swift 2中使用.lineHeightMultiple來壓縮按鈕上的標題文本。

let style = NSMutableParagraphStyle() 
    style.lineHeightMultiple = 0.8 
    style.alignment = .Center 
    style.lineBreakMode = .ByWordWrapping 

    let dict1:[String:AnyObject] = [ 
     NSParagraphStyleAttributeName: style, 
     NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue 
    ] 

    let attrString = NSMutableAttributedString() 
    attrString.appendAttributedString(NSAttributedString(string: "Button Text here over two lines", attributes: dict1)) 
    myButton.setAttributedTitle(attrString, forState: .Normal) 
    myButton.titleLabel?.numberOfLines = 0