gpt4 book ai didi

ios - UIButton:在标题旁边放置图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:01:04 26 4
gpt4 key购买 nike

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

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

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

enter image description here enter image description here enter image description here enter image description here

我做错了什么?

最佳答案

看来你的计算有误,试一试。

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

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

Add an image inside UIButton as an accesory

关于ios - UIButton:在标题旁边放置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30425600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com