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?