博客> 左文字右图片,上图片下文字的UIButton,及细节
左文字右图片,上图片下文字的UIButton,及细节
2020-01-17 07:24 评论:0 阅读:643 superjunjun
UIButton 左文字右图片 上图片下文字 细节

以前碰到这种情况总是使用一个UIButton上面添加一个UILabel 和UIImageView。虽然知道可以使用setTitleEdgeInsets:UIEdgeInsetsMake和 setImageEdgeInsets:UIEdgeInsetsMake 但是一直用的有问题。 问题的原因在于这几个点: 1:首先确定UIButton的size。也就是uibutton的最大限度的size 2:设置好title和imageView后,要先设置setTitleEdgeInsets后设置setImageEdgeInsets 3:如果文字内容是动态的,就要计算好偏移。

//左文字右图片 UIButton默认的是左图片右文字,setTitleEdgeInsets 所以文字的left往左移imageView的宽度,所以是-,它的right边距增加了imageView的宽度,所以是+ setImageEdgeInsets对应 当文字过长的时候,因为已经限制了button的大小,文字会自动缩减,比如乌鲁木齐市 就会缩减为乌....市,美团等应用就是这样处理的。 { UIButton classButton = [UIButton buttonWithType:UIButtonTypeCustom]; classButton.frame = CGRectMake(left + buttonWidth i, 0, buttonWidth, 45); classButton.adjustsImageWhenHighlighted = NO; [classButton.titleLabel setFont:[BOAssistor defaultTextStringFontWithSize:16.0]]; [classButton setTitle:topStringArr[i] forState:UIControlStateNormal]; [classButton setTitleColor:[UIColor colorWithHex:0x848484] forState:UIControlStateNormal]; [classButton setImage:[UIImage imageNamed:@"p_down"] forState:UIControlStateNormal]; [classButton setTitleColor:cCommonRedColor forState:UIControlStateSelected]; [classButton setImage:[UIImage imageNamed:@"hp_locationUp"] forState:UIControlStateSelected]; // CGFloat labelWidth = [classButton.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:[BOAssistor defaultTextStringFontWithSize:16.0]}].width; //// CGFloat labelHeight = [btn.titleLabel.text sizeWithFont:btn.titleLabel.font].height; // [classButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -classButton.imageView.size.width, 0, classButton.imageView.size.width)]; [classButton setImageEdgeInsets:UIEdgeInsetsMake(0, classButton.titleLabel.size.width + 2, 0, -(classButton.titleLabel.size.width + 2))]; [classButton addTarget:self action:@selector(classButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [topBarView addSubview:classButton]; [_buttonArray addObject:classButton]; }

//上图下文字 设置edge的时候要先告诉button contentHorizontalAlignment { UIButton button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(left + i (buttonWidth + cap), shareLabel.bottom + left, buttonWidth, buttonHeight); [button setImage:[UIImage imageNamed:btnImages[i]] forState:UIControlStateNormal]; [button setTitle:textArray[i] forState:UIControlStateNormal]; [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [button.titleLabel setFont:[BOAssistor defaultTextStringFontWithSize:13.0]]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; // button.contentMode = UIViewContentModeCenter; [button setTitleEdgeInsets:UIEdgeInsetsMake(button.imageView.frame.size.height + 10, -button.imageView.frame.size.width, 0.0,0.0)]; [button setImageEdgeInsets:UIEdgeInsetsMake(-button.titleLabel.bounds.size.height, button.titleLabel.bounds.size.width,0, 0)]; [button addTarget:self action:@selector(shareBarButtonAction:) forControlEvents:UIControlEventTouchUpInside]; [shareBar addSubview:button]; [shareButtons addObject:button]; }

收藏
0
sina weixin mail 回到顶部