UIButton different alignment for title and image

南楼画角 提交于 2020-01-05 10:29:01

问题


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?


回答1:


set these two. 1. set Align controls of UIButton

  1. set Image and Text Inset of UIButton




回答2:


The first thought is that you could build a UIView showing image and text, then attach a UITapGestureRecognizer to it, which would be more flexible than UIButton.




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/28340575/uibutton-different-alignment-for-title-and-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!