如何在条形按钮项目的第二行文本下方添加下划线?

How to just add underline below the second line of text in a Bar Button Item?

这就是我想要实现的:

我正在考虑使用两个单独的属性字符串并将它们组合在一起。不确定这是不是唯一的方法?

更新

如果使用 setAttributedTitle,该按钮将显示“(null)”。如果使用setTitle,它可以显示正确的没有属性的字符串。

还是无法正常显示。有什么想法吗?

// Set current bar button attributes
NSMutableAttributedString *currentBarAttributedString = [[NSMutableAttributedString alloc] init];
[currentBarAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"REQUEST\n"
                                                                         attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone)}]];
[currentBarAttributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@"EQUIPMENT"
                                                                                   attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)}]];
// Initialize buttons and set titles
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setAttributedTitle:currentBarAttributedString forState:UIControlStateNormal];
// [button1 setTitle:[currentBarAttributedString string] forState:UIControlStateNormal];

只需创建一个 NSAttributedString 并根据需要对其进行格式化

NSString *alertString = @"All heroes do not wear capes.";

NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentLeft;

NSDictionary *attrs = @{
NSParagraphStyleAttributeName: paragraphStyle,
                        //provide a nsdict here with attributes you want to apply to the whole of the string. 
                        };
NSDictionary *subAttrs = @{
                           NSParagraphStyleAttributeName: paragraphStyle,
                           //Here provide attributes for the not underlined part of the string. 
                           };
NSDictionary *subAttrs2 = @{
                           NSParagraphStyleAttributeName: paragraphStyle,
                           //Here provide attributes for the underlined part of the string
                           };

//Set the range of the sub attributes. 
const NSRange range = NSMakeRange(0,3);
const NSRange range2 = NSMakeRange(5,4);

NSMutableAttributedString *attributedText =
[[NSMutableAttributedString alloc] initWithString:alertString
                                       attributes:attrs];
[attributedText setAttributes:subAttrs range:range];
[attributedText setAttributes:subAttrs2 range:range2];

现在将此属性字符串设置为您的属性标题

class UnderlinedLabel: UILabel {

        override var text: String! {

            didSet {
                 let textRange = NSMakeRange(0, count(text))
                let textRange = NSMakeRange(0, text.characters.count)
                let attributedText = NSMutableAttributedString(string: text)
                attributedText.addAttribute(NSUnderlineStyleAttributeName , value:NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
                // Add other attributes if needed

                self.attributedText = attributedText
             }
        }
    }

为文本添加边框或更改 color.here 是使用的示例代码。 在

中使用此代码
- (void)viewDidLoad {
  [super viewDidLoad];

  NSString *strFirst = @"Request Equipment";
  NSString *strSecond = @"Active Rentals";


NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];

[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:strFirst
                                                                         attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
                                                                            NSForegroundColorAttributeName:[UIColor yellowColor]}]];
[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:strSecond
                                                                         attributes:@{NSUnderlineStyleAttributeName: @(NSUnderlineStyleNone),NSForegroundColorAttributeName:[UIColor whiteColor]}]];
//To use attribute string in button
[self.btnAttributeString setAttributedTitle:attributedString forState:UIControlStateNormal];
}

输出是

请检查这个并让我知道任何问题。