UILabel: Custom underline color?

前端 未结 5 2024
谎友^
谎友^ 2021-01-02 03:22

I need the text of a UILabel to be black, but have a light gray underline under the text. Is this possible with NSAttributedString or TTTAttr

5条回答
  •  庸人自扰
    2021-01-02 03:37

    You can do with NSAttributedString as below.

    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;
    

    Note: You can also underline particular range of string as like in this post. Also note down, it works ios6+ only.

提交回复
热议问题