26

我有一个UIView我想添加几位文本的内容。我用过 aUITextView但我认为这有点过头了,因为它不需要是可编辑的。我考虑过使用 aUILabel或 a UITextField,但我不明白您如何告诉超级视图将UILabelorUITextField放置在自身内部的位置。我想要最小的足迹对象,它可以让我将我选择的字体/颜色/大小的文本放在UIView我想要的位置。不用问太多了吧?

4

7 回答 7

63

对您来说最简单的方法是:

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

在您的代码中构建或填充视图可能需要您CGRectMake大量使用。正如它的名字所说,它创建了一个矩形,您可以使用它来设置相对位置(相对于您的超级视图的边框)和您的UIView-Subclass(在本例中为 a UILabel)的大小。

它是这样工作的:

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

x定义父视图的左边框与您要添加的子视图的开头之间的间距,同样适用于y但与您的父视图的上边框之间的间距有关。那么我认为宽度和高度是不言自明的。

希望这能让你走上正轨。

于 2010-07-09T07:18:39.433 回答
8

无需找到一种方法来告诉视图将 UILabel 放置在哪里,您可以通过使用“中心”来告诉 UILabel 在视图中将自己定位在哪里。

例如

myLabel.center = CGPointMake(0.0, 0.0);

希望您能够使用 UILabel,对我来说它是灵活的不可编辑文本的基本形式。

于 2010-07-09T04:44:48.323 回答
4

对于斯威夫特:

    let yourLabel = UILabel(frame: CGRectMake(100, 100, 100, 100))
    yourLabel.textColor = UIColor.whiteColor()
    yourLabel.backgroundColor = UIColor.blackColor()
    yourLabel.text = "mylabel text"
    yoursuperview.addSubview(yourLabel)
于 2014-11-23T16:14:05.000 回答
2

这个问题很老,但是对于不使用 UILabel 或 UITextField 的纯 UIView 文本选项(正如所有其他答案所描述的,但问题是如何在没有它们的情况下做到这一点),子类 UIView 中的 drawRect 对我有用。像这样:

 - (void)drawRect:(CGRect)rect{
     NSString *string = @"Hello World!";
     [string drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:16.0]];
 }
于 2012-11-06T22:35:48.740 回答
1

此例程在 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];
}
于 2012-12-06T20:23:21.773 回答
1

可能为时已晚,但这是我使用的:-

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;
于 2015-03-30T07:18:24.173 回答
0

将 UILabel 添加到您的视图中。然后覆盖视图的 layoutSubviews 方法。

于 2010-07-09T04:32:47.580 回答