【问题标题】:UIButton with title under the imageview图像视图下带有标题的 UIButton
【发布时间】:2012-09-28 01:20:38
【问题描述】:

我想以编程方式创建一个 UIButton,标题位于 imageView 下。

按钮尺寸:170 * 120 图片尺寸:50 * 50 标题大小:取决于文本。

我知道我必须使用,但我不知道如何:

[_button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f)];
[_button setImageEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, 0.f, 0.f)];

我想我应该计算标题的大小,然后使用 EdgeInsets。

谢谢。

【问题讨论】:

    标签: ios image text uibutton uiedgeinsets


    【解决方案1】:

    希望对你有帮助。

    @interface UIButton (UIButtonExt)  
    
    - (void)centerImageAndTitle:(float)space;  
    - (void)centerImageAndTitle;  
    
    @end  
    
    @implementation UIButton (UIButtonExt)  
    
    - (void)centerImageAndTitle:(float)spacing  
    {      
        // get the size of the elements here for readability  
        CGSize imageSize = self.imageView.frame.size;  
        CGSize titleSize = self.titleLabel.frame.size;  
    
        // get the height they will take up as a unit  
        CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);  
    
        // raise the image and push it right to center it  
        self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);  
    
        // lower the text and push it left to center it  
        self.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (totalHeight - titleSize.height),0.0);      
    }  
    
    - (void)centerImageAndTitle  
    {  
        const int DEFAULT_SPACING = 6.0f;  
        [self centerImageAndTitle:DEFAULT_SPACING];  
    }  
    
    @end   
    

    【讨论】:

    • 这是一个非常好的答案!但我遇到了一些问题,改变你定义 titleSize 的方式解决了它:CGSize titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(self.frame.size.width, MAXFLOAT) lineBreakMode:self.titleLabel.lineBreakMode];
    • @NatanR.,由于 sizeWithFont 现在已弃用,可以使用它来支持 iOS7+ CGSize titleSize = [self.titleLabel.text sizeWithAttributes: @{NSFontAttributeName:self.titleLabel.font}]; by @chadkouse
    • 有没有办法在故事板中做到这一点?
    【解决方案2】:

    最终稳定的解决方案是使用框架,而不是EdgeInset这样的解决方案:

    @interface UIButton (UIButtonExt)
    (void)centerImageAndTitleEx;
    @end
    
    @implementation UIButton (UIButtonExt)
    
    (void)centerImageAndTitleEx
    {
    CGRect frame = self.imageView.frame;
    
    frame = CGRectMake(truncf((self.bounds.size.width - frame.size.width) / 2), 10.0f, frame.size.width, frame.size.height);
    
    self.imageView.frame = frame;
    
    frame = self.titleLabel.frame;
    
    frame = CGRectMake(truncf((self.bounds.size.width - frame.size.width) / 2), self.bounds.size.height - frame.size.height - 5.0, frame.size.width, frame.size.height);
    
    self.titleLabel.frame = frame;
    }
    
    @end
    

    【讨论】:

    • 好像标题文字太长,即使UIButton框架有空间显示,也会在中间截断。每次我尝试扩大标题框架宽度时,标题都会回到图像本身。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2011-05-11
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多