UIEdgeInset issue positioning text/image

一世执手 提交于 2019-12-23 22:06:36

问题


I try to make an UIButton with image upper the text, so I use this code:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x, y, 140, 140);
[btn setTitle:self.objMateria.titoloMateria forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setImage:[UIImage imageNamed:@"pala.png"] forState:UIControlStateNormal];
btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(120, 0, 0, 0);

the problem is that with this code I don't see the text, that seems to be covered by the image, where is the mistake?

here the image:


回答1:


Try this

btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(120, -120, 0, 0);

Adjust the value of -120 i.e. left to horizontally fit your title




回答2:


Can you please modify your code as follows?

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 20, 140, 140);
[btn setTitle:@"pala" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setImage:[UIImage imageNamed:@"pala.png"] forState:UIControlStateNormal];
btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
UIImage *imageTemp = [UIImage imageNamed:@"pala.png"];
btn.titleEdgeInsets = UIEdgeInsetsMake(imageTemp.size.height, -imageTemp.size.width, 0, 0);
[self.view addSubview:btn];


来源:https://stackoverflow.com/questions/13915898/uiedgeinset-issue-positioning-text-image

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