【问题标题】:UIButton: place image next to the titleUIButton:将图片放在标题旁边
【发布时间】:2015-08-06 04:22:48
【问题描述】:

我需要在标题标签旁边(右侧)放置一个图像视图, 所以我用以下方法创建了一个类别:

- (void)updateInsetsToMoveImageRightToText:(NSString *)text {
    CGFloat textWidth = [text sizeWithAttributes:[self.titleLabel.attributedText attributesAtIndex:0 effectiveRange:NULL]].width;
    CGFloat newXPosition = (self.frame.size.width - (self.frame.size.width - textWidth) / 2);
    self.imageEdgeInsets = UIEdgeInsetsMake(0., newXPosition, 0., 0.);
    self.titleEdgeInsets = UIEdgeInsetsMake(0., 0., 0., self.imageView.image.size.width);
}

我是这样使用的:

- (void)quizzesFilterView:(QuizzesFilterView *)filterView didSelectFilter:(QuizFilterType)type {
    self.filterType = type;
    NSString *newTitle = [QuizzesFilterView filterNameByType:type];
    [self.filterButton setTitle:newTitle forState:UIControlStateNormal];
    [self.filterButton updateInsetsToMoveImageRightToText:newTitle];
    [self removeFilterView];
    [self updateSearchedQuizzesWithSearchText:nil quizFilterType:type];
    [self updateTableUi];
}

但它不适用于较小的标题:

我做错了什么?

【问题讨论】:

  • 你在哪里调用该代码?基于 frame 的计算是错误的,例如在 viewDidLoad 中(即当实际帧尚未计算时)
  • @Azat 每次过滤器值发生变化时我都会调用它,它是从放置在按钮下方的表格视图中选择的。

标签: ios objective-c iphone cocoa-touch uibutton


【解决方案1】:

我知道这可能是一个老问题,并用 Objective-C 标记,但对于那些在 Google 上搜索而不指定 iOS 语言的人(如我),这里是 Swift 3.0 版本.

func setupButton(button: UIButton) {
    if let title: NSString = button.currentTitle as NSString?,
        let attribute = button.titleLabel?.attributedText?.attributes(at: 0, effectiveRange: nil) {
        let textWidth = title.size(attributes: attribute).width
        // place here the remaining configurations
    }
}

【讨论】:

    【解决方案2】:

    我编写了一个自定义 UIButton,只需几行代码即可支持灵活对齐。
    这里有:https://github.com/namanhams/FlexibleAlignButton

    示例:
    self.btn.alignment = kButtonAlignmentLabelLeft self.btn.gap = 5; // horizontal gap between image and text

    【讨论】:

      【解决方案3】:

      以下代码对我有用。我在 didMoveToSuperview 中添加了一些代码,以便在视图首次出现时正确布局(我没有对故事板中的按钮进行任何更改,除了设置标题和图像)。

      @implementation RDButton
      
      -(void)didMoveToSuperview {
          CGFloat textWidth = [self.currentTitle sizeWithAttributes:[self.titleLabel.attributedText attributesAtIndex:0 effectiveRange:NULL]].width;
          self.titleEdgeInsets = UIEdgeInsetsMake(0, - (self.imageView.frame.size.width + textWidth), 0, 0);
          self.imageEdgeInsets = UIEdgeInsetsMake(0, textWidth, 0, 0);
          self.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
      }
      
      
      
      - (void)updateInsetsToMoveImageRightToText:(NSString *)text {
          CGFloat textWidth = [text sizeWithAttributes:[self.titleLabel.attributedText attributesAtIndex:0 effectiveRange:NULL]].width;
          self.imageEdgeInsets = UIEdgeInsetsMake(0, textWidth, 0, 0);
      }
      

      编辑后:

      另一种在更改标题时不需要任何调整的方法是,将按钮的图层围绕 y 轴翻转 180 度,并对标题标签的图层执行相同操作以将其翻转回正确读取。这可以在按钮的代码中完成,也可以在控制器的viewDidLoad 中完成,无需子类化按钮。

      - (void)viewDidLoad {
          [super viewDidLoad];
          self.filterButton.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
          self.filterButton.titleLabel.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
      }
      

      【讨论】:

        【解决方案4】:

        看来你的计算可能有误,试试这个。

        - (void)updateInsetsToMoveImageRightToText:(NSString *)text {
            CGFloat textWidth = [text sizeWithAttributes:[self.titleLabel.attributedText attributesAtIndex:0 effectiveRange:NULL]].width;
            CGFloat newXPosition = (self.frame.size.width / 2) + (width / 2); //left offset
            self.imageEdgeInsets = UIEdgeInsetsMake(0.0, newXPosition, 0.0, 0.0); 
            self.titleEdgeInsets = UIEdgeInsetsMake(0., 0., 0., self.imageView.image.size.width);
        }
        

        如果这不能解决问题,您可以将图像添加为按钮的子视图,并根据文本的宽度更轻松地定位它。

        Add an image inside UIButton as an accesory

        【讨论】:

        • 你的表达方式比较好读,但是是一样的 (self.frame.size.width - (self.frame.size.width - textWidth) / 2)
        猜你喜欢
        • 2022-01-19
        • 2014-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-10
        • 2023-02-15
        • 2019-01-07
        • 1970-01-01
        相关资源
        最近更新 更多