我有一个UIView
,想要在上面添加几个文本。我使用了一个UITextView
,但我认为这太过于复杂了,因为它不需要是可编辑的。我考虑使用一个UILabel
或UITextField
,但我不知道如何告诉父视图在其中的哪个位置放置UILabel
或UITextField
。我希望使用最小的占用空间对象,以让我将我选择的字体/颜色/大小的文本放在我想要的UIView
中。这并不是什么难事,不是吗?
我有一个UIView
,想要在上面添加几个文本。我使用了一个UITextView
,但我认为这太过于复杂了,因为它不需要是可编辑的。我考虑使用一个UILabel
或UITextField
,但我不知道如何告诉父视图在其中的哪个位置放置UILabel
或UITextField
。我希望使用最小的占用空间对象,以让我将我选择的字体/颜色/大小的文本放在我想要的UIView
中。这并不是什么难事,不是吗?
对于您来说最简单的方法是:
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
,但是它关联的是父视图顶部边框之间的间距。
然后,宽度和高度应该很容易理解。
希望这能让你明白。
与其寻找一种方法告诉视图如何定位UILabel,不如使用“center”告诉UILabel在视图中自行定位。
例如:
myLabel.center = CGPointMake(0.0, 0.0);
希望您能够使用UILabel,对于我来说,它是一种基本的灵活的不可编辑文本形式。
对于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)
这个问题比较老,但是如果想要在没有使用UILabel或UITextField的情况下(因为所有其他答案都描述了它们的用法,但问题是如何在没有它们的情况下完成),对于一个纯UIView文本选项,我使用子类化UIView中的drawRect方法。像这样:
- (void)drawRect:(CGRect)rect{
NSString *string = @"Hello World!";
[string drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:16.0]];
}
可能有点晚了,但这是我使用的内容:
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;
此例程在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];
}