151 votes

Souligner le texte, dans UIButton

Quelqu'un peut-il m'aider en soulignant le titre de la UIButton. J'ai un UIButton de type Personnalisé, et je veux le Titre pour être souligné, mais l'Interface Constructeur ne fournit pas de possibilité de le faire.

Dans Interface Builder lorsque vous sélectionnez l'Option de Police pour un Bouton, il fournit l'option pour sélectionner Aucun, Simple, Double, de la Couleur, mais aucun de ces fournir toute modification du Titre sur le Bouton.

Toute aide appréciée.

Merci

130voto

Nick H247 Points 2840

Depuis iOS6, il est maintenant possible d'utiliser un NSAttributedString pour effectuer le soulignement (et rien d'autre attribuée chaînes de soutien) d'une façon plus flexible:

NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] initWithString:@"The Quick Brown Fox"];

[commentString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [commentString length])];

[button setAttributedTitle:commentString forState:UIControlStateNormal];

Note: ajouté une autre réponse - comme une solution complètement différente de ma précédente.

79voto

Nick H247 Points 2840

juste besoin de faire moi-même:

UIUnderlinedButton.h

@interface UIUnderlinedButton : UIButton {

}


+ (UIUnderlinedButton*) underlinedButton;
@end

UIUnderlinedButton.m

@implementation UIUnderlinedButton

+ (UIUnderlinedButton*) underlinedButton {
    UIUnderlinedButton* button = [[UIUnderlinedButton alloc] init];
    return [button autorelease];
}

- (void) drawRect:(CGRect)rect {
    CGRect textRect = self.titleLabel.frame;

    // need to put the line at top of descenders (negative value)
    CGFloat descender = self.titleLabel.font.descender;

    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    // set to same colour as text
    CGContextSetStrokeColorWithColor(contextRef, self.titleLabel.textColor.CGColor);

    CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y + textRect.size.height + descender);

    CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);

    CGContextClosePath(contextRef);

    CGContextDrawPath(contextRef, kCGPathStroke);
}


@end

29voto

Rinku Points 361

C'est très simple avec attribuée chaîne

Crée un dictionnaire avec un ensemble d'attributs et de les appliquer à l'attribuée chaîne. Ensuite, vous pouvez définir l'attribuée chaîne attibutedtitle dans uibutton ou attributedtext dans uilabel.

NSDictionary *attrDict = @{NSFontAttributeName : [UIFont
 systemFontOfSize:14.0],NSForegroundColorAttributeName : [UIColor
 whiteColor]};
 NSMutableAttributedString *title =[[NSMutableAttributedString alloc] initWithString:@"mybutton" attributes: attrDict]; 
[title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0,[commentString length])]; [btnRegLater setAttributedTitle:title forState:UIControlStateNormal];

13voto

annie Points 827

Nick la réponse est un grand, moyen rapide de le faire.

J'ai ajouté la prise en charge en drawRect pour les ombres.

Nick la réponse n'est pas à prendre en compte si votre bouton de titre a une ombre en dessous du texte:

enter image description here

Mais vous pouvez déplacer la ligne vers le bas par la hauteur de l'ombre de la sorte:

CGFloat descender = self.titleLabel.font.descender;
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGFloat shadowHeight = self.titleLabel.shadowOffset.height;
descender += shadowHeight;

Ensuite, vous obtiendrez quelque chose comme ceci:

enter image description here

4voto

Rohit Points 19
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
CGRect textRect = self.titleLabel.frame;

// need to put the line at top of descenders (negative value)
CGFloat descender = self.titleLabel.font.descender;

CGContextRef contextRef = UIGraphicsGetCurrentContext();
UIColor *colr;
// set to same colour as text
if (self.isHighlighted || self.isSelected) {
    colr=self.titleLabel.highlightedTextColor;
}
else{
    colr= self.titleLabel.textColor;
}
CGContextSetStrokeColorWithColor(contextRef, colr.CGColor);

CGContextMoveToPoint(contextRef, textRect.origin.x, textRect.origin.y +        textRect.size.height + descender);

CGContextAddLineToPoint(contextRef, textRect.origin.x + textRect.size.width, textRect.origin.y + textRect.size.height + descender);

CGContextClosePath(contextRef);

CGContextDrawPath(contextRef, kCGPathStroke);
}
//Override this to change the underline color to highlighted color
-(void)setHighlighted:(BOOL)highlighted
{
[super setHighlighted:highlighted];
// [self setNeedsDisplay];
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X