English 中文(简体)
UIButton to be resized
原标题:

I am using a UIButton in my application. This button title is displayed dynamically as per the user choice. Now i have to resize the button width as per the title content. I am calculating the string length and assigning it to button s width.

the button is resized as per title but the problem is as follows, 1. If at first the title with large content is shown and after that if a title with a small content is shown means the button is overwritten it doesn t remove the previously constructed value. 2. it looks as if one button is overlapped on the another.

问题回答

Assuming, you have your attribute button in your class

-(void)changeTitleButtonWithValue:(NSString *)value
{
//get the value length (- (CGSize)sizeWithFont:(UIFont *)font)
size = [value sizeWithFont:yourFont];
self.button.frame = CGRectMake(x, y, size.width,size.height);
[self.button setTitle:value forState:UIControlStateNormal];
[self.button setTitle:value forState:UIControlStateHighlighted];

}

Edit : you have many ways to get your button

  • Your create it from Interface Builder
    • Add it as an Outlet
    • Set it a tag and getIt with UIButton *myButton = (UIButton *)[self.view viewForTag:YOURTAG];
  • You create it from code
    • Instance attribute
    • Same thing with tag but when you create it : myButton.tag = YOURTAG;

To me, it sounds like you are not changing the width of the button but you are adding another button. Are you adding the buttons in code? If you do, could you post the code?

In general, if you want to add another button, it would be best, to give it a tag and remove the view with that tag (i.e. the old button) from the superview before adding the new.

This should look something like this (from the top of my head):

UIButton * button = [[[UIButton alloc] init] autorelease];
button.tag = 100;
/* ...set up the button the way you would normally do... */

// remove old button
[[targetView viewWithTag: 100] removeFromSuperview];

// add new button
[targetView addSubview: button];

The tag is an arbitrary integer used to identify a view. It would be best to #define your used tags in a central place.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签