プログラム的にUViewにテキストを追加する方法
-
13-09-2020 - |
質問
私はテキストのいくつかのビットを追加したいというUIView
を持っています。私はUITextView
を使用しましたが、編集可能である必要がないので、それは過剰労働者だと思います。UILabel
またはUITextField
を使用することについて考えていましたが、SuperViewの指示方法は表示されません。私は自分のUILabel
で選択したフォント/カラー/サイズのテキストをテキストに入れて欲しいのですが。尋ねるには多すぎない、eh?
解決
あなたのための最も簡単なアプローチは次のようになるでしょう:
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
SuperViewの左側の境界とサブビューの先頭の間隔を定義します。また、同じ、追加の追加は、y
に適用されますが、スーパービューの上枠の間隔に関連しています。
それから幅と高さは私が考えています。
これがトラック上であなたを得ることを願っています。
他のヒント
Uilbelを配置する場所を知るような方法を見つけるのではなく、 "Center"を使用してビューで自分自身を位置決めするためにUilbelに指示することができます。
e.g.
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で作用してください。そのように:
- (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;
. あなたの見解にUilbelを追加します。次に、ビューのLayoutSubViewsメソッドを上書きします。