10

我有一个简单的查询,我需要在 UIButton 文本和颜色下划线。我有 uibutton 的行文本,但是关于色调它不起作用。我已经从 xib 设置了色调颜色,它没有从那里获取。下面是我的代码。

 NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"Testing"];
[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];
[_helpWithLoginLabel setAttributedTitle:commentString forState:UIControlStateNormal];
4

5 回答 5

9

在斯威夫特:

let attributes = [NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]
let attributedText = NSAttributedString(string: button.currentTitle!, attributes: attributes)

button.titleLabel?.attributedText = attributedText
于 2015-07-14T15:16:49.660 回答
8

NSForegroundColorAttributeNametextColor ( )有一个单独的属性。这是一个例子:

NSArray * keys = [[NSArray alloc] initWithObjects:NSForegroundColorAttributeName, NSUnderlineStyleAttributeName, nil];
NSArray * objects = [[NSArray alloc] initWithObjects:self.descriptionTextView.textColor, [NSNumber numberWithInt:NSUnderlineStyleSingle], nil];
NSDictionary * linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
self.descriptionTextView.linkTextAttributes = linkAttributes;

希望这可以帮助...

于 2013-10-08T19:53:06.000 回答
7

与使用文字的@dequin 相同的响应确实得到更短的语法:

NSDictionary * linkAttributes = @{NSForegroundColorAttributeName:button.titleLabel.textColor, NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:button.titleLabel.text attributes:linkAttributes];            
[button.titleLabel setAttributedText:attributedString];
于 2015-10-08T17:48:37.440 回答
5

根据 Alfie 的回答,我炮制了这个解决方案:

NSArray * objects = [[NSArray alloc] initWithObjects:self.aButton.titleLabel.textColor, [NSNumber numberWithInt:NSUnderlineStyleSingle], nil];
NSArray * keys = [[NSArray alloc] initWithObjects:NSForegroundColorAttributeName, NSUnderlineStyleAttributeName, nil];

NSDictionary * linkAttributes = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];

NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:self.aButton.titleLabel.text attributes:linkAttributes];

[self.aButton.titleLabel setAttributedText:attributedString];
于 2014-12-10T14:04:19.037 回答
1

尝试这个:-

 extension UIButton {

    func UnderlineTextButton(title: String?, forState state: UIControlState)
    {
        self.setTitle(title, for: .normal)
        self.setAttributedTitle(self.attributedString(), for: .normal)
    }

    private func attributedString() -> NSAttributedString? {
        let attributes = [
            NSFontAttributeName : UIFont.systemFont(ofSize: 12.0),
            NSForegroundColorAttributeName : UIColor.black,
            NSUnderlineStyleAttributeName : NSUnderlineStyle.styleSingle.rawValue
            ] as [String : Any]
        let attributedString = NSAttributedString(string: self.currentTitle!, attributes: attributes)
        return attributedString
    }
}

用途:-

button.UnderlineTextButton(title: "Click Here", forState:
UIControlState.normal)
于 2017-06-17T09:41:23.587 回答