如何以编程方式将文本添加到 UIView

发布于 2024-09-08 22:12:27 字数 302 浏览 4 评论 0原文

我有一个 UIView,我想向其中添加几位文本。我使用了 UITextView 但我认为这太过分了,因为它不需要可编辑。我考虑过使用 UILabelUITextField,但我不知道如何告诉超级视图将 UILabel放置在何处>UITextField 本身。我想要占用空间最小的对象,它可以让我将我选择的字体/颜色/大小的文本放置在我的 UIView 中我想要的位置。没什么好问的,是吗?

I have a UIView that I'd like to add several bits of text to. I have used a UITextView but I think that's overkill as it doesn't need to be editable. I thought about using a UILabel or a UITextField, but I don't see how you tell the superview where to position the UILabel or UITextField within itself. I want the lowest footprint object that will let me put text of a font/color/size of my choosing in my UIView where I want it. Not too much to ask, eh?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

红尘作伴 2024-09-15 22:12:27

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

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 的相对位置(相对于超级视图的边框)和大小(在本例中为 UILabel )。

它的工作原理如下:

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

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

希望这能让你走上正轨。

The simplest approach for you would be:

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

Building or populating Views in your code will probably require you to use CGRectMake a lot.
As its name says, it creates a rectangle that you can use to set the relative position (relative to the borders of your superview) and size of your UIView-Subclass (in this case a UILabel).

It works like this:

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

x defines the spacing between the left hand border of the superview and the beginning of the subview your about to add, same applies to y but relating to the spacing between top-border of your superview.
then width and height are self-explanatory i think.

Hope this gets you on the track.

遥远的绿洲 2024-09-15 22:12:27

您可以使用“center”来告诉 UILabel 在视图中的位置,而不是找到一种方法来告诉视图将 UILabel 放置在何处。

例如,

myLabel.center = CGPointMake(0.0, 0.0);

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

Instead of finding a way to tell the view where to position the UILabel, you can tell the UILabel where to position itself in the view by using "center".

E.g.

myLabel.center = CGPointMake(0.0, 0.0);

Hope you'll be able to use UILabel, for me it's the basic form of a flexible non editable text.

栖竹 2024-09-15 22:12:27

对于斯威夫特:

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

For 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)
方圜几里 2024-09-15 22:12:27

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

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

This question is old, but for a pure UIView text option without using UILabel or UITextField (as all the other answers describe, but the question is how to do it without them), drawRect in a subclassed UIView works for me. Like so:

 - (void)drawRect:(CGRect)rect{
     NSString *string = @"Hello World!";
     [string drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:16.0]];
 }
黯然#的苍凉 2024-09-15 22:12:27

此例程在 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];
}

This routine displays a text at a X-Y position

-(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];
}
爱本泡沫多脆弱 2024-09-15 22:12:27

可能会晚,但这是我使用的:-

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;

It might be late but here is what I use:-

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;
栖迟 2024-09-15 22:12:27

将 UILabel 添加到您的视图中。然后重写View的layoutSubviews方法。

add a UILabel to your View. then override the View's layoutSubviews method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文