美文网首页iOS
工作中小遇button的文字和图片间距问题

工作中小遇button的文字和图片间距问题

作者: 只为此心无垠 | 来源:发表于2016-04-11 16:58 被阅读344次

//设置文字图片间距
[self.videoButton.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[self.videoButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
[self.videoButton setTitleColor:UIColorFromRGB(0x525252)
forState:UIControlStateNormal];
self.videoButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5 , 0, 0);
self.videoButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15 , 0 , 15);
_btn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
_btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10.0, 0, 0);

如果不设置内容对齐方式,因为内容文字有可能包含各类字符,大小不等,导致一定误差,图片和文字之间距离大于5,因为内容“火星包:亚索嗜血屠杀之亚索嗜血屠杀2”,尾部刚好有个数字“2”,导致不准确如图:
屏幕快照 2016-04-11 下午4.50.58.png

所以应该在设置间距前,确定文字对齐方式

//设置文字图片间距
[self.videoButton.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[self.videoButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
[self.videoButton setTitleColor:UIColorFromRGB(0x525252)
forState:UIControlStateNormal];
//不可直接调整titleEdgedInsets,直接调整的话会是不准的,要先将按钮内容左对齐或其他方式对齐,再进行边距调整

self.videoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;

self.videoButton.titleEdgeInsets = UIEdgeInsetsMake(0, 5 , 0, 0);
self.videoButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15 , 0 , 15);

不可直接调整titleEdgedInsets,直接调整的话会是不准的,要先将按钮内容左对齐或其他方式对齐,再进行边距调整。

相关文章

网友评论

    本文标题:工作中小遇button的文字和图片间距问题

    本文链接:https://www.haomeiwen.com/subject/daeilttx.html