【问题标题】:Image and Text locations in UIButtonUIButton 中的图像和文本位置
【发布时间】:2015-06-05 01:21:17
【问题描述】:

我试图将UIImage 和文本放入UIButton,通过使用UIEdgeInset 来定位它们。

问题是,更改其中一个 edgeinset 设置将导致图像和文本都更改位置。 也就是说,我把图片放到了正确的位置,然后我改变了titleEdgeInset的值,突然不仅文字而且图片也改变了位置。

有人可以帮忙吗?

【问题讨论】:

  • 请详细说明,分享一些代码,说明您在哪里以及如何更改插图以及位置发生了什么
  • 您可以尝试创建一个新的自定义按钮类并实现 UIButton,通过在自定义按钮上添加标签和图像作为其属性来覆盖 UIButton,导入该类,然后在您的视图上对其进行初始化以使用它. init 语句看起来像 -> CustomButton*custBtn = [[CustomButton alloc] initWithFrame:frame];

标签: objective-c uibutton uiedgeinsets


【解决方案1】:

根据 Apple 的文档,这不会发生。您是否检查以确保您有负插入而不是正插入?

您指定的插入将在标题矩形的大小调整为适合按钮的文本后应用到该矩形。因此,正的插入值实际上可能会剪切标题文本。

此属性仅用于在布局期间定位标题。 按钮不使用此属性来确定 intrinsicContentSize 和 sizeThatFits:。

【讨论】:

    【解决方案2】:

    希望对你有帮助

    @property (strong, nonatomic) IBOutlet UIButton *Imagetext;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Click  Me"];
    
    NSTextAttachment *ImageAttachment = [[NSTextAttachment alloc] init];
    ImageAttachment.image = [UIImage imageNamed:@"add.png"];
    
    NSAttributedString *attributedWithImage = [NSAttributedString attributedStringWithAttachment:ImageAttachment];
    
    [attributedString replaceCharactersInRange:NSMakeRange(6, 0) withAttributedString:attributedWithImage];
    //    [attributedString appendAttributedString:attributedWithImage];
    
    [self.Imagetext setAttributedTitle:attributedString forState:UIControlStateNormal];
    

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 2011-01-31
      • 2015-05-27
      • 2016-10-02
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多