【问题标题】:Set Button title in two different colors in iOS在 iOS 中将按钮标题设置为两种不同的颜色
【发布时间】:2013-06-24 19:06:04
【问题描述】:

我想在按钮上设置两种颜色的标题。

是否可能与下图相同?

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 使用属性文本作为标题..
  • @MidhunMP:不,我会从网络服务获取文本。所以,不能设置为图片。
  • 您可以将图像与文本一起用于按钮
  • 是添加图片作为 UIButton 的 backgroundImage
  • 放图片而不是给文字着色会很容易...

标签: iphone ios uibutton uicolor


【解决方案1】:
[button setAttributedTitle:someAttributedText forState:UIControlStateNormal];

【讨论】:

    【解决方案2】:

    在 storyBoard 中:跟随屏幕截图。

    对于编程:这里已经有很多关于属性文本的答案帖子,如果你想把 BackgroundImage for UIButton Set a button background image iPhone programmatically

    【讨论】:

    • KI:我可以在从 Web 服务响应中获取记录后以编程方式设置吗?
    • 感谢截图。我没有意识到我必须突出显示框中的文本才能改变颜色。无法弄清楚为什么我的设置颜色不起作用!
    【解决方案3】:

    试试这个

    NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc] initWithString:@"ThisIsAttributed"];
    [mutableString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0,4)];
    [mutableString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor]  range:NSMakeRange(5,2)];
    [button setAttributedTitle:mutableString forState:UIControlStateNormal];
    

    【讨论】:

      【解决方案4】:

      这就是你需要的

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(5,10,300,20);
        [self.view addSubview:btn];
      
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"100 Photos"]];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
        [btn setAttributedTitle:attrStr forState:UIControlStateNormal];
      

      【讨论】:

      • @iManan- 你的目标操作系统是什么
      • 对于 Xamarin 使用 UIStringAttributeKey.ForegroundColor 作为属性名 NSString
      【解决方案5】:

      您可以使用for ios below 5.0NSMutableAttributedString 用于当前的 ios 版本。

      【讨论】:

        【解决方案6】:

        对于 Swift 3:

        let button = UIButton()
        let mainTitle = "200"
        let subtitle = "Members"
        
        let attrText1 = NSMutableAttributedString(string: mainTitle)
        attrText1.addAttribute(NSFontAttributeName, value: 15, range: NSMakeRange(0, attrText1.length))
        attrText1.addAttribute(NSForegroundColorAttributeName, value: UIColor.black, range: NSMakeRange(0, attrText1.length))
        
        
        let attrText2 = NSMutableAttributedString(string: subtitle)
        attrText2.addAttribute(NSFontAttributeName, value: 15, range: NSMakeRange(0, attrText2.length))
        attrText2.addAttribute(NSForegroundColorAttributeName, value: UIColor.yellow, range: NSMakeRange(0, attrText2.length))
        
        let attributedText = NSMutableAttributedString()
        attributedText.append(attrText1)
        attributedText.append(attrText2)
        
        //Center align
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
        
        attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0,attributedText.length))
        
        button.setAttributedTitle(attributedText, for: .normal)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-25
          • 1970-01-01
          • 1970-01-01
          • 2015-12-07
          • 2015-03-11
          • 1970-01-01
          • 2011-12-19
          • 2012-02-27
          相关资源
          最近更新 更多