Ios 获取UIButton的大小并右对齐

Ios 获取UIButton的大小并右对齐,ios,ios7,uibutton,Ios,Ios7,Uibutton,我有这样一个按钮: UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeSystem]; [inviteButton setTitle:@"Invite" forState:UIControlStateNormal]; [inviteButton.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:(14.0)]]; [inviteButto

我有这样一个按钮:

UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeSystem];
[inviteButton setTitle:@"Invite" forState:UIControlStateNormal];
[inviteButton.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:(14.0)]];
[inviteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
如何获取其高度和宽度,以便设置其框架

如果我这样做:

float width = inviteButton.frame.size.width;
然后宽度始终为零


我希望按钮在我创建的视图中右对齐。

正如我看到的,您没有设置按钮的帧。所以你得到的是零回报。如果没有设置帧,则无法获得宽度,因此首先使用如下设置帧

inviteButton.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);;
inviteButton.frame=CGRectMake(,);;

正如我看到的,您没有设置按钮的框架。所以你得到的是零回报。如果没有设置帧,则无法获得宽度,因此首先使用如下设置帧

inviteButton.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>);;
inviteButton.frame=CGRectMake(,);;

您必须使用这样的框架设置UIButton的高度和宽度

    inviteButton.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
inviteButton.frame=CGRectMake(,)

您必须使用这样的框架设置UIButton的高度和宽度

    inviteButton.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
inviteButton.frame=CGRectMake(,)

我实际上想要这个:

这是不赞成的

CGSize size = [[inviteButton titleForState:UIControlStateNormal] sizeWithFont:inviteButton.titleLabel.font];
这是新的方式:

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:(14.0)]};

CGSize size = [[inviteButton titleForState:UIControlStateNormal] sizeWithAttributes:attributes];

我真的想要这个:

这是不赞成的

CGSize size = [[inviteButton titleForState:UIControlStateNormal] sizeWithFont:inviteButton.titleLabel.font];
这是新的方式:

NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:(14.0)]};

CGSize size = [[inviteButton titleForState:UIControlStateNormal] sizeWithAttributes:attributes];

您可以使用此设置宽度和高度

[尺寸合适]

之后试着打印这个

浮动宽度=button.frame.size.width

然后将按钮的框架设置为在屏幕上特定位置显示


[inviteButton setFrame:CGRectMake(x,y,inviteButton.frame.size.width,inviteButton.frame.size.height)]

您可以使用此设置宽度和高度

[尺寸合适]

之后试着打印这个

浮动宽度=button.frame.size.width

然后将按钮的框架设置为在屏幕上特定位置显示

[inviteButton setFrame:CGRectMake(x,y,inviteButton.frame.size.width,inviteButton.frame.size.height)]