프로그래밍 방식으로 UIView에 텍스트를 추가하는 방법
-
13-09-2020 - |
문제
나는 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을 사용할 수 있기를 바랍니다. 저에게는 편집 불가능한 유연한 텍스트의 기본 형식입니다.
스위프트의 경우:
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]];
}
이 루틴은 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];
}
늦었을 수도 있지만 여기에 내가 사용하는 것이 있습니다.
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;
뷰에 UILabel을 추가하세요.그런 다음 View의layoutSubviews 메서드를 재정의합니다.