当我使用带有按钮标题的不支持时,UIButton标题闪烁

时间:2016-02-11 10:13:51

标签: ios objective-c

我在下面的代码中使用带下划线的按​​钮标题

 NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
[btn setAttributedTitle:[[NSAttributedString alloc] initWithString:@"See Event TnC" attributes:underlineAttribute] forState:UIControlStateNormal];

3 个答案:

答案 0 :(得分:1)

Hello vikramarkaios您可以在属性enter image description here按钮中将UIButton类型系统更改为自定义

我希望它对你有所帮助!!

答案 1 :(得分:0)

您可以在设置按钮标题之前在UIView上禁用动画。

[UIView setAnimationsEnabled:NO];
NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
[btn setAttributedTitle:[[NSAttributedString alloc] initWithString:@"See Event TnC" attributes:underlineAttribute] forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];

答案 2 :(得分:0)

从iOS6开始,现在可以使用NSAttributedString以更灵活的方式执行下划线(以及其他任何归因于字符串支持的内容):

NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"The Quick Brown Fox"];

[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];

注意:将此添加为另一个答案 - 因为它与我之前的解决方案完全不同。

奇怪的是(至少在iOS8中)你必须为第一个字符加下划线,否则它不起作用!

以便解决方法,将第一个字符串设置为带有明确颜色的下划线!

NSMutableAttributedString* tncString = [[NSMutableAttributedString alloc] initWithString:@"View Terms and Conditions"];

    // workaround for bug in UIButton - first char needs to be underlined for some reason!
    [tncString addAttribute:NSUnderlineStyleAttributeName
                      value:@(NSUnderlineStyleSingle)
                      range:(NSRange){0,1}];
    [tncString addAttribute:NSUnderlineColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0, 1)];


    [tncString addAttribute:NSUnderlineStyleAttributeName
                      value:@(NSUnderlineStyleSingle)
                      range:(NSRange){5,[tncString length] - 5}];

    [tncBtn setAttributedTitle:tncString forState:UIControlStateNormal];