【发布时间】:2015-04-11 11:31:41
【问题描述】:
我有一个 UIButton。在那个 UIButton 中,我必须显示文本和图像。图像应显示在文本之后。基于文本我想调整按钮框架。 看图片。在文本“我是按钮”之后,我想添加一个像箭头这样的图像。
UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *name = @"I am a button";
titleButton.titleLabel.text = [NSString stringWithFormat:@"%@", name];
titleButton.titleLabel.textColor = [UIColor whiteColor];
CGSize stringSize = [name sizeWithAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"MyriadPro-Cond" size:24]}];
CGRect frame = CGRectMake(300, 30, stringSize.width, 30);
//frame.size.width = stringSize.width;
[titleButton setFrame:frame];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home.png"]];
image.frame = CGRectMake(stringSize.width, 14, 20, 13);
titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, stringSize.width+20, 0);
[titleButton addSubview:image];
我写了这样的代码。但它不起作用。任何人都可以使它正确。提前致谢
【问题讨论】:
-
尝试玩转 UIButton 的
imageEdgeInsets和titleEdgeInsets属性。你一定会得到你需要的。
标签: ios objective-c xcode uiimageview uibutton