简体   繁体   English

以编程方式更改UIButton的标题,其标题在IB中设置为归属

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

I have changed titles of UIButton s before using: 我在使用之前更改了UIButton的标题:

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

But I've run into a situation where the UIButton I'm using has a mult-line title and will only center the text properly if changed from "plain" to "attributed" in IB. 我遇到的情况是我正在使用的UIButton有一个多行标题,只有在IB中从“普通”变为“归属”时才会正确居中。 When the title is changed in this way, the usual setTitle: forState: method no longer updates the button. 当以这种方式更改标题时,通常的setTitle: forState:方法不再更新按钮。

How do you make changes to an attributed title of a UIButton ? 如何更改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 UIButton and 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 attributed in IB. 问题是只要标题设置为IB中的属性 ,它就不会改变按钮的外观。 My code works fine by simply changing this to plain . 只需将其更改为plain,我的代码就可以正常工作。 But this is not a solution as described in the link above. 但这不是上面链接中描述的解决方案。

Attributed titles are obtained via 归属标题是通过

- (NSAttributedString *)attributedTitleForState:(UIControlState)state

and set via 并设置通过

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

Formatting the NSAttributedString object is a bit complicated, but setting it to nil will clear the title and that's what I needed. 格式化NSAttributedString对象有点复杂,但将其设置为nil将清除标题,这就是我所需要的。

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)? 您是否在视图控制器类中将按钮指定为IBOutlet,并且它是否正确连接为Interface Builder中的插座(从新引用插座拖动到文件所有者并选择您的UIButton对象)? That's usually the problem I have when I see these symptoms .specify first - @property(non atomic,strong)IBOutlet UIButton *mybutton; 当我看到这些症状时,这通常是我遇到的问题。首先指定 - @property @property(non atomic,strong)IBOutlet UIButton *mybutton;

then 然后

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

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进行更改

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)

I had to use the following 4 lines to get the attributed font to display. 我必须使用以下4行来显示属性字体。 In IB the title is set to plain and not attributed. 在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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM