Programmatically change title of UIButton whose ti

2019-02-07 22:42发布

I have changed titles of UIButtons before using:

- (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. When the title is changed in this way, the usual setTitle: forState: method no longer updates the button.

How do you make changes to an attributed title of 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 UIButton and logging the output of

- (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. My code works fine by simply changing this to plain. But this is not a solution as described in the link above.

5条回答
Summer. ? 凉城
2楼-- · 2019-02-07 23:09

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.

// 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];
查看更多
Root(大扎)
3楼-- · 2019-02-07 23:18

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

let attributedStringForInvalid = NSAttributedString(string: "I'm Invalid!", attributes: [
  NSStrikethroughStyleAttributeName: true,
  NSForegroundColorAttributeName: UIColor.redColor()
  ])
myButton.setAttributedTitle(attributedStringForInvalid, forState: UIControlState.Normal)
查看更多
迷人小祖宗
4楼-- · 2019-02-07 23:19

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.

查看更多
贪生不怕死
5楼-- · 2019-02-07 23:23

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

查看更多
一纸荒年 Trace。
6楼-- · 2019-02-07 23:24

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;

then

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

查看更多
登录 后发表回答