[英]UIButton title blinking when iam using undreline with button title
我在下面的代碼使用帶下划線的按鈕標題
NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
[btn setAttributedTitle:[[NSAttributedString alloc] initWithString:@"See Event TnC" attributes:underlineAttribute] forState:UIControlStateNormal];
您可以在設置按鈕標題之前在UIView上禁用動畫。
[UIView setAnimationsEnabled:NO];
NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
[btn setAttributedTitle:[[NSAttributedString alloc] initWithString:@"See Event TnC" attributes:underlineAttribute] forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];
從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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.