El texto proviene de una base de datos. Me gustaría usarlo para un botón y subrayar el texto del botón. ¿Cómo puedo hacer eso?¿Cómo puedo subrayar el texto del botón de UIButton?
Respuesta
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];
}
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.
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];
¿Podemos hacer la línea más baja? – onmyway133
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)
}
}
- 1. Obtener UIButton texto del emisor
- 2. ¿Cómo subrayar el texto en reStructuredText?
- 3. ¿Cómo puedo cambiar el color del título de UIButton?
- 4. UIButton texto del título truncado
- 5. título UIButton color del texto
- 6. Subrayar texto en Python/Matplotlib
- 7. ¿Puedo subrayar el texto de un NSTextField del constructor de interfaz?
- 8. Cambiar el tamaño del texto en un UIButton
- 9. Cómo desactivar la sombra del botón al pulsar el botón UIButton?
- 10. obtener el texto del botón presionado
- 11. ¿Cómo cambiar el tamaño del texto del botón UI en monotouch?
- 12. Animación del botón de compra de UIButton AppStore
- 13. ¿Cambiar el texto del botón de xcode?
- 14. ¿Cómo puedo personalizar el color del texto del botón Atrás en una vista de encabezado UINavigationController?
- 15. Maximizar el tamaño del texto del botón de Android
- 16. iPhone iOS cómo agregar gradiente lineal a un UIButton debajo del texto o la imagen del botón?
- 17. Cómo cambiar el texto del botón de retroceso de uinavigationbar
- 18. iOS UIButton con UITextAlignmentLeft sigue centrando el texto?
- 19. ¿Cómo establecer el color del texto en el botón Enviar?
- 20. Cambiar el texto del botón del teclado "Volver"
- 21. Puede subrayar palabras en TextView texto
- 22. cómo establecer el margen de texto interno del botón
- 23. Cómo configurar el texto del botón de imagen (Android)?
- 24. ¿Cómo cambio el color del texto de un botón?
- 25. El cambio de texto en UIButton no se pega
- 26. ¿Cómo subrayar encabezados de sección en LaTeX?
- 27. ¿Cómo puedo dar el enlace externo a UIButton? ¿Es posible?
- 28. Cómo ajustar el texto del botón html con ancho fijo
- 29. Borrar el color del botón en el campo de texto
- 30. Cómo cambiar el tamaño de UIButton
Aquí es cómo hacerlo mismo en guión gráfico (XCode 6). http://stackoverflow.com/a/26930512/309046 – satish