【问题标题】:UIButton - Lead Text with IconUIButton - 带图标的引导文本
【发布时间】:2013-10-16 11:42:36
【问题描述】:

我正在尝试找出一种创建 UIButton 的解决方案,它的文本居中,UIImage(图标)引导文本,因此它的站点就在按钮标题的前面。

但是,我正在努力想办法做到这一点,因为您无法检索文本的位置。有什么想法吗?这一定是一件相当普遍的事情。

【问题讨论】:

  • 检索文本位置是什么意思。
  • 好吧,如果我可以检索第一个字符的 x 位置,那么我只需将图像的位置适当地放在所述文本之前。
  • 你可以继承 uibutton 并在其上添加 uiimageview 和 uilabel 以获得文本和所有属性,你也可以自定义它。

标签: ios objective-c uibutton uiimage


【解决方案1】:

使用此代码

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 设置文本的起点和终点。

这样你的图片会在最左边,你可以在图片后面写文字

【讨论】:

    【解决方案2】:

    创建具有UIImageViewUILabelUIView 子类。将标签放置在此视图中图像视图的右侧。将此视图添加到您的按钮并将其水平和垂直居中放置。

    【讨论】:

      【解决方案3】:

      我想,它会对你有所帮助。始终小心setImage:setBackgroundImage:

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

      【讨论】:

        【解决方案4】:

        使用以下方法:

        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];
        

        这里是参考:Add an image inside UIButton as an accesory

        【讨论】:

          【解决方案5】:

          你也可以继承 UIButton 这是一个更好的方法。

          【讨论】:

            【解决方案6】:

            如果你像我一样使用字体图像,你可以使用 NSParagraphStyle

            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/

            这部分是一种将char转换为字符串的方法的一部分

            [IcoMoon iconString:k12_ADD_NOTE]
            

            您可以前往https://github.com/sebastienwindal/IOSIcoMoon了解更多信息

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-02-15
              • 2017-11-09
              • 1970-01-01
              • 2018-04-14
              • 2018-08-08
              • 1970-01-01
              • 2013-02-09
              • 1970-01-01
              相关资源
              最近更新 更多