【问题标题】:UIButton set Text on top of imageUIButton 在图像顶部设置文本
【发布时间】:2015-05-27 08:47:27
【问题描述】:

如何在已经包含图像的按钮中添加文本。我尝试了各种方法,但文本不可见。如何使文本在图像顶部可见?

谁能帮帮我!

【问题讨论】:

  • 使用 [myButton setBackgroundImage:myImage] 代替 setImage 属性;然后使用 setTitle。希望这会有所帮助。

标签: ios objective-c iphone ipad uiview


【解决方案1】:

我找到了更好的方法。

UILabel *next=[ [UILabel alloc] initWithFrame:CGRectMake(0,0,375,65)];
next.text=@"SIGN IN";
next.textAlignment = NSTextAlignmentCenter;
next.font=[UIFont fontWithName:@"Helvetica" size:20.0 ];
next.textColor=[UIColor whiteColor];
[self.button addSubview:next];

【讨论】:

    【解决方案2】:

    如果您的按钮图像设置为 setImage 状态,则文本不会显示在 Button 上。

    如果你必须在按钮上显示名称,那么我们必须设置 setBackgroundImage 然后你可以设置按钮的标题。

    [btn setBackgroundImage:YOURIMAGE forState:UIControlStateNormal];
    [btn setTitle:YOURTITLE forState:UIControlStateNormal];
    

    【讨论】:

      【解决方案3】:

      对我来说很好用的东西:我在 InterfaceBuilder 中设置了按钮,并使用“位置”附近的小图标来定义文本的位置。鼠标悬停显示由 InterfaceBuilder 应用的代码。 现在如果文本没有显示,由于颜色,我使用对按钮的引用并设置属性标题:

      NSDictionary *atts = [[NSDictionary alloc]initWithObjectsAndKeys:[NSColor whiteColor], NSForegroundColorAttributeName, nil];
          NSAttributedString *example = [[NSAttributedString alloc]initWithString:@"myText" attributes:atts];
          [myExampleButton setAttributedTitle:example];
      

      【讨论】:

        【解决方案4】:
        [yourButton setBackgroundImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];
        [yourButton setTitle:@"Click" forState:UIControlStateNormal];
        

        这必须有效。

        【讨论】:

        • 在我写答案的时候,有很多答案。 :D 这显示了 iOS 社区的活力:)
        【解决方案5】:

        将按钮上的背景图片和标题设置为-

        [self.m_myBetButton setBackgroundImage:[UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
            [self.m_myBetButton setTitle:@"BUTTON TITLE" forState:UIControlStateNormal];
        

        【讨论】:

          【解决方案6】:

          你需要设置按钮的背景图片,而不是图片,使用setBackgroundImage:forState:

          【讨论】:

            【解决方案7】:

            尝试设置按钮的backgroundImage属性

            [self.buttonWithTextAndImage setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
            [self.buttonWithTextAndImage setTitle:@"Button Title" forState:UIControlStateNormal]; 
            

            【讨论】:

              猜你喜欢
              • 2016-12-09
              • 2015-06-15
              • 1970-01-01
              • 2019-05-23
              • 1970-01-01
              • 1970-01-01
              • 2021-12-23
              • 2013-10-14
              • 1970-01-01
              相关资源
              最近更新 更多