iOS-5中UIButton标题中带下划线的多行

时间:2014-02-07 11:30:50

标签: ios iphone ios6 ios7 uibutton

我想在iOS中创建下划线按钮链接。 enter image description here

我已经使用过UIUnderlineButton

所以它给了我以下输出。

enter image description here

请帮助我实现第1张图像下划线按钮。

3 个答案:

答案 0 :(得分:2)

这应该适用于iOS6及更高版本,使用referencedString:

NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"yourTextButton"];
[attrStr addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, [attrStr length])];
[yourButton setAttributedTitle:attrStr forState:UIControlStateNormal];
[[yourButton titleLabel] setNumberOfLines:0];//Multiple Lines

答案 1 :(得分:0)

为什么不在文字很长的情况下使用UILabel和UITapGestureRecognizer,而且不需要任何额外的课程。

答案 2 :(得分:0)

检查一下。我希望这是你想要的。

NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:@"Tap here...."];
    [titleString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [titleString length])];
    [titleString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [titleString length])];
    [_button setAttributedTitle: titleString forState:UIControlStateNormal];