UIButton - 带图标的带领文本

时间:2013-10-16 11:42:36

标签: ios objective-c uibutton uiimage

我试图想出一个创建UIButton的解决方案,其中心文本有一个UIImage(图标)引导文本,所以它的网站就在按钮标题的前面。

然而,我正在努力想办法这样做,因为你无法检索文本的位置。有什么想法吗?这一定是相当常见的事情。

6 个答案:

答案 0 :(得分:2)

使用此代码

UIButton *scoreButton=[UIButton buttonWithType:UIButtonTypeCustom];
scoreButton.frame=CGRectMake(0,0,100, 100);

//scoreButton.contentEdgeInsets=UIEdgeInsetsMake(0, 0, 0, 2);

scoreButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;

scoreButton.titleLabel.font=[UIFont boldSystemFontOfSize:13];
[scoreButton setTitleColor:[UIColor colorWithRed:0.9411 green:0.5647 blue:.2916 alpha:YES] forState:UIControlStateNormal];


[scoreButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];


UIImageView *scoreButtonImageView=[[UIImageView alloc]init];
scoreButtonImageView.frame=CGRectMake(0,35,30 ,30);
scoreButtonImageView.image=[UIImage imageNamed:@"leaderboard_score_button.png"];
[scoreButton addSubview:scoreButtonImageView];

使用UIEdgeInsetsMake设置文本的起点和终点。

通过这种方式,您的图像将位于最左侧,您可以在图像之后写入文本

答案 1 :(得分:1)

创建一个UIView子类,其中包含UIImageViewUILabel。将标签置于此视图中图像视图的右侧。将此视图添加到按钮并将其水平和垂直居中定位。

答案 2 :(得分:1)

我想,它会对你有所帮助。请务必小心setImage:setBackgroundImage:

UIButton *yourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[yourBtn setBackgroundImage:bgImage forState:UIControlStateNormal];
[yourBtn setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[yourBtn setTitle:titleString forState:UIControlStateNormal];

答案 3 :(得分:1)

使用以下方法:

UIImageView *yourPlusSign = [UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourPlusSignImageTitle"];
yourPlusSign.frame = CGRectMake(x, y, width, height);//choose values that fit properly inside the frame of your baseButton
//or grab the width and height of yourBaseButton and change accordingly
yourPlusButton.contentMode=UIViewContentModeScaleAspectFill;//or whichever mode works best for you
[yourBaseButton addSubview:yourPlusSign];

这是Ref:Add an image inside UIButton as an accesory

答案 4 :(得分:0)

你也可以将UIButton子类化,这是一种更好的方法。

答案 5 :(得分:0)

如果您使用像我这样的字体图像,您可以使用NSParagraphStyle执行此操作 Add Note

NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];

UIFont *font1 = [UIFont fontWithName:@"Avenir-Roman" size:14.0];
UIFont *font2 = [UIFont fontWithName:@"Icon-Moon" size:12.0];

    NSDictionary *fontSyle1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
                            NSFontAttributeName:font1,
                            NSParagraphStyleAttributeName:style};
    NSDictionary *fontStyle2 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
                            NSFontAttributeName:font2,
                            NSParagraphStyleAttributeName:style};

    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:[IcoMoon iconString:k12_ADD_NOTE] attributes:fontStyle2]];
    [attString appendAttributedString:[[NSAttributedString alloc] initWithString:@" Add Note" attributes:fontStyle1]];

    [_addNewBtn setAttributedTitle:attString forState:UIControlStateNormal];
    [[_addNewBtn titleLabel] setNumberOfLines:0];
    _addNewBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _addNewBtn.layer.borderWidth = 1;
    [_addNewBtn setBackgroundColor:[UIColor whiteColor]];
    [_addNewBtn setTintColor:[UIColor darkGrayColor]];

您可以在此处获取图标字体https://icomoon.io/

此部分是将字符串转换为字符串

的方法的一部分
[IcoMoon iconString:k12_ADD_NOTE]

您可以转到https://github.com/sebastienwindal/IOSIcoMoon

了解更多信息