Domanda

Ho un UIView a cui vorrei aggiungere diversi bit di testo.Ho usato un UITextView ma penso che sia eccessivo in quanto non ha bisogno di essere modificabile.Ho pensato di usare un UILabel o un UITextField, ma non vedo come dice alla superview dove posizionare il UILabel o UITextField dentro di sé.Voglio l'oggetto footprint più basso che mi permetta di inserire il testo di un font/colore/dimensione di mia scelta nel mio UIView dove voglio.Non chiedere troppo, eh?

È stato utile?

Soluzione

L'approccio più semplice per te sarebbe:

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

La creazione o la compilazione di viste nel codice richiederà probabilmente l'utilizzo CGRectMake sacco.Come dice il nome, crea un rettangolo che puoi usare per impostare la posizione relativa (relativa ai bordi della tua superview) e la dimensione del tuo UIView-Subclass (in questo caso a UILabel).

Funziona così:

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

x definisce la spaziatura tra il bordo sinistro della superview e l'inizio della sottoview che stai per aggiungere, lo stesso vale per y ma relativo alla spaziatura tra il bordo superiore della tua superview.quindi larghezza e altezza sono auto-esplicative penso.

Spero che questo ti porti in pista.

Altri suggerimenti

Invece di trovare un modo per raccontare la vista in cui posizionare l'Uilabel, puoi dire a Uilabel dove posizionarsi nella vista usando "Center".

E.G.

myLabel.center = CGPointMake(0.0, 0.0);
.

Spero che sarai in grado di usare Uilabel, per me è la forma di base di un testo flessibile non modificabile.

per 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)
.

Questa domanda è vecchia, ma per un'opzione di testo UIView pure senza utilizzare Uilabel o UitextField (come descrizione di tutte le altre risposte, ma la domanda è come farlo senza di loro), il rivestito in un uiview subclassato funziona per me.Come così:

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

Questa routine visualizza un testo in una posizione X-Y

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

Potrebbe essere in ritardo ma qui è ciò che 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;
.

Aggiungi un Uilabel alla tua opinione.Quindi sovrascrivere il metodo LayoutSuSuSuSup di View.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top