Image and Text locations in UIButton

320 views Asked by At

Im trying to put a UIImage and text into a UIButton, by using UIEdgeInset to locate them.

The problem is, changing one of the edgeinset setting will cause both the image and the text to change the position. Which means, I put the image to the correct position, then I change the titleEdgeInset values, suddenly not only the text but also the image changed the position.

Any one can help out please?

2

There are 2 answers

0
Mohamed Jaleel Nazir On

I hope this may helps you

@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];
0
lead_the_zeppelin On

According the Apple's documentation this won't happen. Did you check to make sure you have negative insets and not positive?

The insets you specify are applied to the title rectangle after that rectangle has been sized to fit the button’s text. Thus, positive inset values may actually clip the title text.

This property is used only for positioning the title during layout. The button does not use this property to determine intrinsicContentSize and sizeThatFits:.