iOS开发--根据文字动态调整按钮(UIButton)宽度

    CGRect mainBounds = [[UIScreen mainScreen] bounds];
    UIFont *font = [UIFont systemFontOfSize:15.0f];
    CGRect locationBtnFrame = self.locationButton.frame;
    
    CGFloat maxWidth = mainBounds.size.width - locationBtnFrame.origin.x;
    CGSize size = CGSizeMake(maxWidth, MAXFLOAT);
    CGSize theStringSize = [theText sizeWithFont:font
                               constrainedToSize:size
                                   lineBreakMode:self.locationButton.lineBreakMode];
    
    self.locationButton.frame = CGRectMake(locationBtnFrame.origin.x,
                                locationBtnFrame.origin.y,
                                theStringSize.width, locationBtnFrame.size.height);

你可能感兴趣的:(iOS开发--根据文字动态调整按钮(UIButton)宽度)