简体   繁体   English

UIButton:将图片放在标题旁边

[英]UIButton: place image next to the title

I need to place an image view next to the title label (on the right side), so I've created a category with following method: 我需要在标题标签旁边(右侧)放置一个图像视图,因此我使用以下方法创建了一个类别:

- (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);
}

I'm using it like this: 我正在这样使用它:

- (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];
}

But it doesn't work for smaller titles: 但这不适用于较小的标题:

在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

What am I doing wrong? 我究竟做错了什么?

Seems your calculation may be wrong, give this a try. 似乎您的计算可能有误,请尝试一下。

- (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);
}

If that doesn't solve the problem, you can add the image as a subview of the button and position it easier according to the width of the text. 如果仍不能解决问题,则可以将图像添加为按钮的子视图,并根据文本的宽度将其轻松放置。

Add an image inside UIButton as an accesory 在UIButton中添加图片作为附件

The following code worked for me. 以下代码对我有用。 I put some code in didMoveToSuperview to get the layout correct when the view first appears (I didn't make any changes to the button in the storyboard, other than to set the title and the image). 我在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);
}

After Edit: 编辑后:

Another way that doesn't require any adjustment when you change the title, is to flip the button's layer 180 degrees around the y axis, and do the same to the title label's layer to flip it back to reading correctly. 更改标题时,无需进行任何调整的另一种方法是,将按钮的图层绕y轴翻转180度,并对标题标签的图层进行相同操作,以将其翻转回到正确的位置。 This can be done in the button's code, or you can do it in viewDidLoad of the controller with no need to subclass the button. 这可以在按钮的代码中完成,也可以在控制器的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);
}

I have written a custom UIButton that supports flexible alignment in just few lines of code. 我编写了一个自定义UIButton,仅需几行代码即可支持灵活对齐。
It's available here : https://github.com/namanhams/FlexibleAlignButton 它可以在这里找到: https : //github.com/namanhams/FlexibleAlignButton

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

I know this might be an old question and tagged with Objective-C , but for those guys (like me) who search on Google without specifying the language of the iOS, here's the Swift 3.0 version . 我知道这可能是一个古老的问题,并用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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM