ios 以编程方式更改标题在 IB 中设置为属性的 UIButton 的标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12652198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:30:41  来源:igfitidea点击:

Programmatically change title of UIButton whose title was set in IB as attributed

iosuibuttoninterface-builder

提问by Jeff Lockhart

I have changed titles of UIButtons before using:

UIButton在使用之前更改了s 的标题:

- (void)setTitle:(NSString *)title forState:(UIControlState)state

But I've run into a situationwhere the UIButtonI'm using has a mult-line title and will only center the text properly if changed from "plain" to "attributed" in IB. When the title is changed in this way, the usual setTitle: forState:method no longer updates the button.

但是我遇到了一种情况UIButton我正在使用的具有多行标题并且只有在 IB 中从“普通”更改为“归因”时才会正确地将文本居中。当标题以这种方式更改时,通常的setTitle: forState:方法不再更新按钮。

How do you make changes to an attributedtitle of a UIButton?

您如何更改a的属性标题UIButton

To clarify, so you don't assume I'm just making a stupid mistake, I've used the debugger to peek at the properties of the UIButtonand logging the output of

澄清一下,所以你不要以为我只是犯了一个愚蠢的错误,我已经使用调试器查看了 的属性UIButton并记录了输出

- (NSString *)titleForState:(UIControlState)state

after using

使用后

- (void)setTitle:(NSString *)title forState:(UIControlState)state

to set it returns the value just set. Problem is it doesn't change the appearance of the button as long as the title is set to attributedin IB. My code works fine by simply changing this to plain. But this is not a solution as described in the link above.

设置它返回刚刚设置的值。问题是只要标题在 IB 中设置为属性,它就不会改变按钮的外观。我的代码只需将其更改为plain即可正常工作。但这不是上面链接中描述的解决方案。

回答by Jeff Lockhart

Attributed titles are obtained via

属性标题通过以下方式获得

- (NSAttributedString *)attributedTitleForState:(UIControlState)state

and set via

并通过设置

- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state

Formatting the NSAttributedStringobject is a bit complicated, but setting it to nilwill clear the title and that's what I needed.

格式化NSAttributedString对象有点复杂,但将其设置为nil将清除标题,这就是我所需要的。

回答by Christien

Do you have the button specified as an IBOutlet in your view controller class, and is it connected properly as an outlet in Interface Builder (ctrl drag from new referencing outlet to file owner and select your UIButton object)? That's usually the problem I have when I see these symptoms .specify first - @property(non atomic,strong)IBOutlet UIButton *mybutton;

您是否在视图控制器类中将按钮指定为 IBOutlet,并且它是否作为接口生成器中的插座正确连接(ctrl 从新引用插座拖到文件所有者并选择您的 UIButton 对象)?这通常是我看到这些症状时遇到的问题。首先指定 - @property(non atomic,strong)IBOutlet UIButton *mybutton;

then

然后

[myButton setTitle: @"myTitle" forState: UIControlStateNormal];

[myButton setTitle: @"myTitle" forState: UIControlStateNormal];

回答by Christien

here's one

这是一个

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
       action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Title" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
 [view addSubview:button];

no need to make changes in- (void)setTitle:(NSString *)title

无需更改- (void)setTitle:(NSString *)title

回答by ObjectiveTC

Swift - For a red strikethrough font in a button title:

Swift - 对于按钮标题中的红色删除线字体:

let attributedStringForInvalid = NSAttributedString(string: "I'm Invalid!", attributes: [
  NSStrikethroughStyleAttributeName: true,
  NSForegroundColorAttributeName: UIColor.redColor()
  ])
myButton.setAttributedTitle(attributedStringForInvalid, forState: UIControlState.Normal)

回答by iCyberPaul

I had to use the following 4 lines to get the attributed font to display. In IB the title is set to plain and not attributed.

我必须使用以下 4 行来显示要显示的属性字体。在 IB 中,标题设置为普通而不是归因。

// Get the library font
UIFont *termsFont = [MTHGAppearanceController defaultFontOfSize:[MTHGAppearanceController defaultButtonFontSizeForDevice]];
// Set the font attributes required, using our librarby function for setting the colour
NSDictionary *fontAttributes = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),NSBackgroundColorAttributeName: [UIColor clearColor], NSFontAttributeName: termsFont, NSForegroundColorAttributeName:[UIColor mthg_color255WithRed:128.0f green:128.0f blue:128.0f alpha:1.0f]};
// Configure the text we want displayed using the font set above
NSAttributedString *termsString = [[NSAttributedString alloc]initWithString:@"Terms" attributes:fontAttributes];
// Now finally display the text in the required font
[self.termsButtonOutlet setAttributedTitle:termsString forState:UIControlStateNormal];