美文网首页
iOS 根据按钮的内容设置宽度

iOS 根据按钮的内容设置宽度

作者: 呦释原点 | 来源:发表于2017-02-15 13:39 被阅读17次
    7.0以前
    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);
    
    7.0以后
    NSString *content = button.titleLabel.text;  
        UIFont *font = button.titleLabel.font;  
        CGSize size = CGSizeMake(MAXFLOAT, 30.0f);  
        CGSize buttonSize = [content boundingRectWithSize:size  
                              options:NSStringDrawingTruncatesLastVisibleLine  | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading  
                           attributes:@{ NSFontAttributeName:font}  
                                                   context:nil].size;  
    

    相关文章

      网友评论

          本文标题:iOS 根据按钮的内容设置宽度

          本文链接:https://www.haomeiwen.com/subject/wurqwttx.html