2011-02-02 9 views
0

自分のコンテンツに合ったUIViewを作成したいと思います。内容は、ダイナミックテキストのUILabelです。コンテンツに応じた適応可能な高さのUIView - Objective-c

UIView *myview = [ [UIView alloc ] initWithFrame:CGRectMake(10.0, 10.0, 700.0, 100.0)]; 
myview.backgroundColor = [UIColor redColor]; 

[self.view addSubview:myview]; 

UILabel *myLabel = [ [UILabel alloc ] initWithFrame:CGRectMake(10.0, 10.0, 500.0, 43.0) ]; 
myLabel.textAlignment = UITextAlignmentLeft; 
myLabel.textColor = [UIColor whiteColor]; 
myLabel.backgroundColor = [UIColor blackColor]; 
myLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(36.0)]; 
[myView addSubview:myLabel]; 
myLabel.numberOfLines = 0; 
myLabel.text = @"This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. "; 
[myLabel sizeToFit]; 

私はスクリーンショットを添付:

Screenshot

答えて

3

[myLabel sizeToFit]後にこれを試してみてください:

[myView setFrame:myLabel.frame]; 
+0

ほぼ!これは問題ありませんが、マージンは維持されません。 Look:http://i54.tinypic.com/15gpmy8.png – HispaJavi

+0

これは解決策ではないと思います。私はこれの下に別のラベルを貼る必要があります。 – HispaJavi

+0

さて、私はそれをやった。ありがとうございました。 – HispaJavi

0

これは、トリックを行います:

NSString *tempString = @"This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. This is an UILabel. "; 

    CGSize constSize = { 260.0f, 20000.0f }; 

CGSize textSizeTitle = [tempString sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:constSize lineBreakMode:UILineBreakModeWordWrap]; 

UILabel *EventTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 260,textSizeTitle.height)]; 
[EventTitle setText:tempString]; 
[EventTitle setBackgroundColor:[UIColor clearColor]]; 
[EventTitle setFont:[UIFont fontWithName:@"TrebuchetMS-Bold" size:20]]; 
[EventTitle setNumberOfLines:0]; 
[EventTitle setUserInteractionEnabled:NO]; 
[EventTitle setTextColor:[UIColor whiteColor]]; 
[newsBox addSubview:EventTitle]; 

textSizeTitleのフォントサイズを& EventTitleと同じにしてください。

関連する問題