简体   繁体   中英

UIButton different alignment for title and image

Hello I am creating UIButton which is displaying image and title at the same time. I want title and image to be both centred, button to be above image. Problem is that because of localisation of string from button, string length is changing, and alignment from UIStoryboard combined with edge insets is not working as I want it to work. Can someone help me and point me in the right direction?

set these two. 1. set Align controls of UIButton

  1. set Image and Text Inset of UIButton

设置UIButton的对齐控件

首先想到的是,您可以构建一个显示图像和文本的UIView,然后将UITapGestureRecognizer附加到该UIView上,这将比UIButton更加灵活。

I am not sure, but you can try to set title like this:

[someButton setTitle:@"\nsome title" forState:UIControlStateNormal];

where \\n is a new line.

Adjust titleEdgeInsets and imageEdgeInsets accordingly

[self.facebookButton setImage:[UIImage imageNamed:@"facebook_icn"] forState:UIControlStateNormal];  
[self.facebookButton setTitle:NSLocalizedString(@"Invite via Facebook1",nil) forState:UIControlStateNormal];
self.facebookButton.titleEdgeInsets = UIEdgeInsetsMake(0.0f, 5.0f, 0.0f, -5.0f);
self.facebookButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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