¿Cómo puedo cambiar el tamaño de letra de una UILabel en Swift?
label.font.pointSize
es de solo lectura, así que no estoy seguro de cómo cambiarlo.
17 answers
Puedes hacerlo así:
label.font = UIFont(name: label.font.fontName, size: 20)
O así:
label.font = label.font.fontWithSize(20)
Esto usará la misma fuente. 20 puede ser cualquier tamaño que desee, por supuesto.
Nota: La última opción sobrescribirá el peso de la fuente actual a regular
, por lo que si desea conservar el peso de la fuente, use la primera opción.
Actualización de Swift 3 :
label.font = label.font.withSize(20)
Actualización de Swift 4 :
label.font = label.font.withSize(20)
O
label.font = UIFont(name:"fontname", size: 20.0)
Y si utiliza las fuentes del sistema
label.font = UIFont.systemFont(ofSize: 20.0)
label.font = UIFont.boldSystemFont(ofSize: 20.0)
label.font = UIFont.italicSystemFont(ofSize: 20.0)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-03 11:09:09
Creo que la mejor manera de hacer esto - si mantener la misma fuente que ya está asignada a la UILabel
sería:
(usando Swift)
label.font = label.font.fontWithSize(20)
(usando Swift 3)
label.font = label.font.withSize(20)
Idealmente establecería esto en el método viewDidLayoutSubviews
, ya que no necesita cambiar cada vez que aparece la vista.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-19 12:21:13
label.font = UIFont.systemFontOfSize(20)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-30 16:48:19
Podemos establecer la fuente según nuestro requisito como,
label.font = UIFont(name: "Avenir-Light", size: 15.0)
label.font = UIFont.boldSystemFontOfSize(15)
label.font = UIFont.italicSystemFontOfSize(15)
label.font = UIFont.systemFontOfSize(17)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-03 10:23:55
Si desea cambiar el tamaño de su fuente, creo esta extensión
// Add extension
extension UILabel {
func setSizeFont (sizeFont: Double) {
self.font = UIFont(name: self.font.fontName, size: sizeFont)!
self.sizeToFit()
}
}
// Use
myLabel.setSizeFont(60)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-17 19:13:20
Usted puede dar así también
labelName.font = UIFont(name: "systemFont", size: 30)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-05 04:57:16
En Swift 3 de nuevo...
myLabel.font = myLabel.font.withSize(18)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-23 19:41:31
Swift-3.1
Etiqueta.font = UIFont.systemFont (ofSize: 12)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-18 07:35:11
En swift3, supongamos que su nombre UILable es myLable y desea cambiar su tamaño de fuente haga esto
myLable.font = UIFont.systemFont(ofSize: 10)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-04 09:16:05
Puede usar una extensión.
import UIKit
extension UILabel {
func sizeFont(_ size: CGFloat) {
self.font = self.font.withSize(size)
}
}
Para usarlo:
self.myLabel.fontSize(100)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-06 11:50:31
Apple sigue cambiando las cosas sin razón: Swift 4+:
myLabel.font = UIFont.systemFont(ofSize: 16)
Gracias Apple por perder tiempo a la gente para averiguar qué métodos de "tamaño de fuente" necesitan usar!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-02 19:33:42
Usé fontWithSize
para una etiqueta con fuente de sistema ligera, pero vuelve a la fuente de sistema normal.
Si desea mantener los rasgos de la fuente, es mejor incluir los descriptores.
label.font = UIFont(descriptor: label.font.fontDescriptor(), size: 16.0)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-06 17:05:52
En Swift 3:
label = UIFont.systemFont(ofSize: 20)
Y para usar tamaños preestablecidos del sistema, por ejemplo:
label = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-13 18:08:02
Swift 3
label.font.withSize(16)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-16 07:51:16
Swift 3.1
import UIKit
extension UILabel {
var fontSize: CGFloat {
get {
return self.font.pointSize
}
set {
self.font = UIFont(name: self.font.fontName, size: newValue)!
self.sizeToFit()
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-26 13:12:56
SWIFT 3.1
Etiqueta.font = Label.letra.withSize (newValue)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-09 07:52:11
Es muy fácil y conveniente cambiar el tamaño de fuente de storyboard, y puede ver instantáneamente el resultado del cambio.
En realidad, también es muy fácil cambiar otros atributos de fuente en el guion gráfico, como el estilo, la familia de fuentes, etc.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-13 17:57:52