2011-03-26 25 views

Respuesta

4

Para ello se puede subclase UILabel y sobrescribir su método -drawRect y luego utilizar su propio UILabel y añadir un UIButton sobre ella de tipo personalizado.

Haga su método drawRect en UILabel como

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetRGBStrokeColor(context, 207.0f/255.0f, 91.0f/255.0f, 44.0f/255.0f, 1.0f); 

    CGContextSetLineWidth(context, 1.0f); 

    CGContextMoveToPoint(context, 0, self.bounds.size.height - 1); 
    CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height - 1); 

    CGContextStrokePath(context); 

    [super drawRect:rect]; 

} 
0

Para hacer esto un poco más simple (es un requisito común) que he construido un simple llamado subclase UIButton BVUnderlineButton que se puede soltar directamente en sus proyectos.

Está en Github en https://github.com/benvium/BVUnderlineButton (licencia de MIT).

Puede usarlo en un XIB/Storyboard o directamente a través de un código.

42

En iOS 6, NSAttributedString se utiliza la modificación del texto, puede utilizar "NSMutableAttributedString" de texto en varios colores, tipo de letra, estilo, etc. usando solo UIButton o UILabel.

NSMutableAttributedString *titleString = [[NSMutableAttributedString alloc] initWithString:@"The Underlined text"]; 

// making text property to underline text- 
[titleString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, [titleString length])]; 

// using text on button 
[button setAttributedTitle: titleString forState:UIControlStateNormal]; 
+1

¿Podemos hacer la línea más baja? – onmyway133

1

En Swift 3 la siguiente extensión se puede utilizar para un subrayado:

extension UIButton { 
    func underlineButton(text: String) { 
     let titleString = NSMutableAttributedString(string: text) 
     titleString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, text.characters.count)) 
     self.setAttributedTitle(titleString, for: .normal) 
    } 
} 
Cuestiones relacionadas