2012-03-20 58 views
1

我想要實現這樣的一個UIView裏面:iOS版 - 創建自定義的UILabel並將其添加到UIView的

enter image description here

我創建了一個自定義的UILabel使用自定義init方法:

@implementation TagLabel 
- (id)init { 
    self = [super initWithFrame:CGRectMake(0, 0, 100, 40)]; 
    if (self) { 
     self.layer.borderColor = [UIColor whiteColor].CGColor; 
     self.layer.borderWidth = 4.0; 
     self.layer.cornerRadius = 4; 
     self.backgroundColor = [UIColor greenColor]; 
     self.textColor = [UIColor whiteColor]; 
     self.textAlignment = UITextAlignmentCenter; 
    } 
    return self; 
} 
@end 

我創建和每個標籤添加到視圖,像這樣:

for (NSDictionary *tag in [self.voicenote objectForKey:@"Tag"]) { 
     TagLabel *tagLabel = [[TagLabel alloc] init]; 
     tagLabel.text = [tag objectForKey:@"tag"]; 
     [self.tagsView addSubview:tagLabel]; 
    } 

ÿ等我得到的是一個空白的UIView,我錯過了什麼?會有更好的方法來實現嗎?

EDIT:解決了一個問題,修改了UILabel代碼,將背景顏色直接設置爲背景,現在他們顯示,現在我怎麼能讓它們成爲一個旁邊而不是重疊?

回答

2

的相同位置在你的循環,你需要定製幀爲標籤並安排他們你想要的方式。 像這樣也許:

CGRect frame = label.frame; 
frame.size.height = masterFrame.height; 
frame.origin.y = masterFrame.origin.y + masterFrame.size.height + 40; 
label.frame = frame; 

這是做一個更長的路。你也可以用[CGRect initWithFrame ...]

來初始化幀
0

他們都加入到父視圖

相關問題