How Can I Underline the Button Text of UIButton?

后端 未结 4 1957
既然无缘
既然无缘 2021-02-03 14:09

The text is coming form a database. I would like to use it for a button and underline the text of the button. How can I do that?

4条回答
  •  半阙折子戏
    2021-02-03 14:34

    In iOS 6, NSAttributedString is used modifying the text, you can use "NSMutableAttributedString" for multi color text, font, style, etc using single UIButton or UILabel.

    NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:@"The Underlined text"];
    
    // making text property to underline text-
    [titleString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [titleString length])];
    
    // using text on button
    [button setAttributedTitle: titleString forState:UIControlStateNormal];
    

提交回复
热议问题