Pregunta

Tengo un UIView que me gustaría añadir varios bits de texto.He utilizado un UITextView pero yo creo que es una exageración, ya que no necesitan ser editable.Pensé acerca del uso de un UILabel o un UITextField, pero no veo la manera de decirle a la superview donde la posición de la UILabel o UITextField dentro de sí mismo.Quiero la menor huella de objeto que me permite poner el texto de una fuente/color/tamaño de mi elección en mi UIView donde yo quiero.No pedir demasiado, ¿eh?

¿Fue útil?

Solución

El enfoque más sencillo para usted sería:

UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];

[yourLabel setTextColor:[UIColor blackColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]]; 
[yourSuperView addSubview:yourLabel];

Edificio o rellenar Vistas en el código probablemente requieren el uso de CGRectMake mucho.Como su nombre lo dice, se crea un rectángulo que puede utilizar para establecer la posición relativa (en relación a las fronteras de su superview) y el tamaño de su UIView-Subclass (en este caso un UILabel).

Esto funciona así:

yourLabel.Frame = CGRectMake(x, y, width, height); //x,y,width,height are float values.

x define el espacio entre la mano izquierda de la frontera de la superview y el comienzo de la subvista su alrededor para agregar, lo mismo se aplica a y pero relacionadas con el espacio entre la parte superior de la frontera de su superview.a continuación, la anchura y la altura son auto-explicativos, creo.

Espero que esto se presenta en la pista.

Otros consejos

En lugar de encontrar una manera de decirle a la vista de cuál es la posición de la UILabel, usted puede decirle a la UILabel donde posicionarse en la vista mediante el uso de "centro".

E. g.

myLabel.center = CGPointMake(0.0, 0.0);

Espero que usted será capaz de utilizar UILabel, para mí es la forma básica de un flexible y no en texto editable.

Para Swift:

    let yourLabel = UILabel(frame: CGRectMake(100, 100, 100, 100))
    yourLabel.textColor = UIColor.whiteColor()
    yourLabel.backgroundColor = UIColor.blackColor()
    yourLabel.text = "mylabel text"
    yoursuperview.addSubview(yourLabel)

Esta pregunta es viejo, pero para una pura UIView opción de texto sin usar UILabel o UITextField (como todas las otras respuestas describir, pero la pregunta es ¿cómo hacerlo sin ellos), drawRect en una subclase de UIView funciona para mí.Así:

 - (void)drawRect:(CGRect)rect{
     NSString *string = @"Hello World!";
     [string drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:16.0]];
 }

Esta rutina se muestra un texto en un X-Y de la posición

-(void)placeText:(NSString *)theText:(int)theX:(int)theY {
    UILabel *textLabel;

    // Set font and calculate used space
    UIFont *textFont = [UIFont fontWithName:@"Helvetica" size:14];
    CGSize textStringSize = [theText sizeWithFont:textFont constrainedToSize:CGSizeMake(300,50) lineBreakMode:NSLineBreakByTruncatingTail];

    // Position of the text
    textLabel = [[UILabel alloc] initWithFrame:CGRectMake(theX+OFFSETIMAGEX-(textStringSize.width/2), theY+OFFSETIMAGEY-(textStringSize.height/2), textStringSize.width,textStringSize.height)];

    // Set text attributes
    textLabel.textColor = [UIColor blackColor];
    textLabel.backgroundColor = [UIColor orangeColor];
    textLabel.font = textFont;
    textLabel.text = theText;

    // Display text
    [self.view addSubview:textLabel];
}

Podría ser tarde, pero aquí es lo que yo uso:-

CGRect labelFrame = CGRectMake(120,300, 530, 100);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
//If you need to change the color
[myLabel setTextColor:[UIColor whiteColor]];
//If you need to change the system font
[myLabel setFont:[UIFont fontWithName:NULL size:23]];
//If you need alignment
[myLabel setTextAlignment:NSTextAlignmentCenter];
// The label will use an unlimited number of lines
[myLabel setNumberOfLines:0];
//Add label view to current view
[self.view addSubview:myLabel];
NSString *someString = @"Sample String, Yarp!";
myLabel.text = someString;

añadir un UILabel de su punto de Vista.a continuación, reemplazar el punto de Vista del layoutSubviews método.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top