without create objective-c xcode text uiview programmatically-created

objective c - create - Cómo agregar texto mediante programación a un UIView



swift without storyboard (7)

Tengo una UIView que me gustaría agregar varios bits de texto. He usado una UITextView pero creo que es exagerada ya que no necesita ser editable. Pensé en usar un UILabel o un UITextField , pero no veo cómo le digas a la supervista dónde colocar el UILabel o el UITextField dentro de sí mismo. Quiero el objeto de huella más bajo que me permita poner texto de una fuente / color / tamaño de mi elección en mi UIView donde lo desee. No hay mucho que preguntar, ¿eh?


El enfoque más simple 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];

Construir o poblar vistas en tu código probablemente requiera que uses CGRectMake mucho. Como su nombre lo indica, crea un rectángulo que puede usar para establecer la posición relativa (relativa a los bordes de su supervista) y el tamaño de su UIView-Subclass (en este caso, un UILabel ).

Funciona así:

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

x define el espaciado entre el borde de la mano izquierda de la supervista y el comienzo de la subvista que está a punto de agregar, lo mismo se aplica a y pero relacionado con el espaciado entre los bordes superiores de su supervista. entonces, el ancho y la altura son autoexplicativos, creo.

Espero que esto te lleve a la pista.


En lugar de encontrar una manera de decirle a la vista dónde ubicar el UILabel, puede decirle a UILabel dónde posicionarse en la vista usando "centro".

P.ej

myLabel.center = CGPointMake(0.0, 0.0);

Espero que puedas usar UILabel, para mí es la forma básica de un texto flexible no editable.


Esta pregunta es antigua, pero para una opción de texto UIView pura sin usar UILabel o UITextField (como todas las otras respuestas describen, pero la pregunta es cómo hacerlo sin ellas), drawRect en una UIView subclasificada funciona para mí. Al igual que:

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


Esta rutina muestra un texto en una posición XY

-(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]; }


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)


Puede ser tarde, pero esto es lo que 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;


agregue un UILabel a su vista. a continuación, anule el método ViewSubviews de View.