如何在Bar Button Item的第二行文字中添加下划线?
这就是我想要达到的目标:如何在Bar Button Item的第二行文字中添加下划线?
我想有两个单独的原因字符串,并将它们结合在一起。不确定这是否唯一的方法?
UPDATE
按钮显示 “(空)” 如果使用setAttributedTitle。如果使用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];
要添加边框的文本或改变颜色格式化。这里是使用的示例代码。 在
- (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];
}
输出使用这段代码是
请检查这一点,让我知道的任何问题。
高兴地帮助你。 –
只需创建一个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
}
}
}
请参阅更新。任何想法? –
解决方案是要求客观的C代码。不在快速 –
http://stackoverflow.com/questions/28053334/how-to-underline-a-uilabel-in-swift –
@KrutarthPatel请参阅更新。任何想法? –
当我做NSLog。数据正在打印。请求 设备 –