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

时间:2016-10-05 05:31:16

标签: ios objective-c swift uibarbuttonitem uitoolbar

这就是我想要实现的目标:

enter image description here

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

更新

如果使用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];

3 个答案:

答案 0 :(得分:1)

要为文本添加边框或更改颜色,请使用示例代码。    在

中使用此代码
- (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];
}

OutPut

enter image description here

请检查此信息并告诉我任何问题。

答案 1 :(得分:0)

只需创建一个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];

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

答案 2 :(得分:-1)

result