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

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


I have changed titles of UIButtons before using:

我在使用之前更改了UIButtons的标题:

- (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.

但是我遇到了一种情况,我正在使用的UIButton有一个多行标题,只有在IB中从“普通”变为“归属”时才会正确居中。当以这种方式更改标题时,通常的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. My code works fine by simply changing this to plain. But this is not a solution as described in the link above.

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

5 个解决方案

#1


58  

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将清除标题,这就是我需要的。

#2


9  

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,并且它是否正确连接为Interface Builder中的插座(从新引用插座拖动到文件所有者并选择您的UIButton对象)?当我看到这些症状时,这通常是我遇到的问题。首先指定 - @property(非原子,强)IBOutlet UIButton * mybutton;

then

然后

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

[myButton setTitle:@“myTitle”forState:UIControlStateNormal];

#3


4  

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 *)标题中进行更改

#4


1  

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)

#5


1  

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];
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/09/29/7dee759d8008777a52fc2e93a2a09749.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告