【问题标题】:UILabel: Custom underline color?UILabel:自定义下划线颜色?
【发布时间】:2014-03-15 05:24:32
【问题描述】:

我需要UILabel 的文本为黑色,但文本下方有浅灰色下划线。 NSAttributedStringTTTAttributedLabel 可以做到这一点吗?还是需要使用 Core Graphics 进行自定义绘图?

澄清: 我需要不同颜色下划线的特定颜色文本。示例:红色下划线上的蓝色文本。

【问题讨论】:

    标签: ios ios7 uilabel nsattributedstring tttattributedlabel


    【解决方案1】:

    您可以使用NSAttributedString,如下所示。

    NSMutableAttributedString* string = [[NSMutableAttributedString alloc]initWithString:@"you string"];
    [string addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, string.length)];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, string.length)];//TextColor
    [string addAttribute:NSUnderlineStyleAttributeName value:underlineNumber range:NSMakeRange(0, string.length)];//Underline color
    [string addAttribute:NSUnderlineColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, string.length)];//TextColor
     yourlabel. attributedText = string;
    

    注意:您还可以在特定范围的字符串下划线为like in this post。另请注意,它仅适用于 ios6+。

    【讨论】:

    • NSUnderlineColorAttributeName 在 iOS 7.0 中可用,对吧?
    【解决方案2】:

    我们总是可以在一行中创建多个属性(使用 NSDictionary 符号 - @ { } ),而不是创建本地 NSMutableAttributedString 并逐个添加属性到特定 UILabel 包括实际的文本。

    目标 C

    [someUILabel setAttributedText:
      [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", [someObject stringProperty]]
                                      attributes:@{NSUnderlineStyleAttributeName:@(NSUnderlineStyleThick),
                                                  NSUnderlineColorAttributeName:[[UIColor alloc] initWithRed:0.953f green:0.424f blue:0.416f alpha:1.00f]}]];
    

    在上面的例子中,我们设置了一个也是粗体的下划线 - 总共 2 个属性。

    斯威夫特

    self.someUIButton.setAttributedTitle(NSAttributedString(string: "UIButtonStringTitle", attributes: [NSUnderlineStyleAttributeName : 1]), forState: .Normal)
    

    【讨论】:

      【解决方案3】:
      // Print `str` in black, and underline the word STRING in gray.
      NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"This is my STRING"];
      [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, str.length-7)];
      [str addAttribute:NSUnderlineColorAttributeName value:[UIColor grayColor] range:NSMakeRange([str length]-6, 6)];
      [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange([str length]-6, 6)];
      
      _label.attributedText = str; // assuming you have an iVar name `label`
      

      【讨论】:

        【解决方案4】:
        NSAttributedString *title;
        
        title = [[NSAttributedString alloc] initWithString:@"iphone app" for NSAttributedString" attributes:@{ NSFontAttributeName : [UIFont fontWithName:@"Noteworthy-Bold" size:36], NSUnderlineStyleAttributeName : @1 , NSStrokeColorAttributeName : [UIColor blackColor]}]; 
        
        UILabel *label;
        
        label = [[UILabel alloc] initWithFrame:CGRectMake( (self.view.bounds.size.width - title.size.width) / 2.0f, 40.0f, title.size.width, title.size.height)];
        label.attributedText = title; 
        [self.view addSubview:label];
        

        【讨论】:

          【解决方案5】:
          m-farhan.com  farhan will be underlined
          
          //-----------------------------
              // Create attributed string
              //-----------------------------
              NSString *str = @"m-Farhan.com";
              NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str];
          
              // Add attribute NSUnderlineStyleAttributeName
              [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleSingle] range:NSMakeRange(2, 4)];
          
              // Set background color for entire range
              [attributedString addAttribute:NSBackgroundColorAttributeName
                value:[UIColor colorWithRed:0.103 green:0.305 blue:0.492 alpha:1.000]
                range:NSMakeRange(0, [attributedString length])];
          
              // Define label
              UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 280, 80)];
              [label setLineBreakMode:UILineBreakModeWordWrap];
              [label setTextColor:[UIColor whiteColor]];
              [label setBackgroundColor:[UIColor clearColor]];
              [label setTextAlignment:UITextAlignmentLeft];
          
              // Set label text to attributed string
              [label setAttributedText:attributedString];
              [[self view] addSubview:label];
          

          【讨论】:

          • 这并没有解释如何获得与下划线颜色不同的文本颜色。
          【解决方案6】:

          在swift中,使用NSAttributedString.Key.underlineColor

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-02-05
            • 1970-01-01
            • 1970-01-01
            • 2017-12-18
            • 2012-09-15
            • 2016-09-03
            • 2019-02-13
            • 2010-09-16
            相关资源
            最近更新 更多