뷰에서 여러 개의 label 가운데 표시
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.textData = @[@"OC", @"Swift", @" ", @" ", @" ", @" ", @" ", @" ", @" ", @" ", @"wawahaha", @"ok"];
self.backgroundView = [[UIView alloc] init];
self.backgroundView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.backgroundView];
[self createLabel];
}
- (void)createLabel {
NSDictionary *dic = [self getFirstLabelLeft:0];
NSInteger changeLineIndex = [dic.allKeys.firstObject integerValue];
CGFloat rectX = [dic.allValues.firstObject floatValue];
CGFloat rectY = 10.0;
//
for (int i = 0; i < self.textData.count; i ++) {
NSString *labelText = self.textData[i];
UILabel *centerLabel = [[UILabel alloc] init];
centerLabel.font = [UIFont systemFontOfSize:10];
centerLabel.textAlignment = NSTextAlignmentCenter;
centerLabel.text = labelText;
centerLabel.textColor = [UIColor brownColor];
centerLabel.layer.borderColor = [UIColor whiteColor].CGColor;
centerLabel.layer.borderWidth = 0.5;
centerLabel.layer.cornerRadius = 2;
centerLabel.layer.masksToBounds = YES;
[centerLabel sizeToFit];
CGFloat rectW = centerLabel.bounds.size.width + 6.0;
if (i > 0) {
// label
NSString *foreLabelText = self.textData[i - 1];
UILabel *foreLabel = [[UILabel alloc] init];
foreLabel.font = [UIFont systemFontOfSize:10];
foreLabel.textAlignment = NSTextAlignmentCenter;
foreLabel.text = foreLabelText;
[foreLabel sizeToFit];
_labelWidth = foreLabel.frame.size.width + 6.0;
// icon
if (i == changeLineIndex) {
rectY = rectY + 10.0 * 2;
NSDictionary *secondDic = [self getFirstLabelLeft:changeLineIndex];
rectX = [secondDic.allValues.firstObject floatValue];
changeLineIndex = [secondDic.allKeys.firstObject integerValue];
} else {
rectX = rectX + _labelWidth + 4.0;
}
}
centerLabel.frame = CGRectMake(rectX, rectY, rectW, centerLabel.bounds.size.height + 10.0 / 2);
centerLabel.tag = i + 100;
[self.backgroundView addSubview:centerLabel];
_centerLabelBottom = CGRectGetMaxY(centerLabel.frame);
}
self.backgroundView.frame = CGRectMake(0.0, 50.0, [UIScreen mainScreen].bounds.size.width, _centerLabelBottom + 10.0);
}
/**
label x
@param index
@return
*/
- (NSDictionary *)getFirstLabelLeft:(NSInteger)index {
CGFloat labelWidth = 0.0;
NSInteger endIndex = 0;
for (NSInteger i = index; i < self.textData.count; i ++) {
NSString *labelText = self.textData[i];
UILabel *centerLabel = [[UILabel alloc] init];
centerLabel.font = [UIFont systemFontOfSize:10];
centerLabel.textAlignment = NSTextAlignmentCenter;
centerLabel.text = labelText;
centerLabel.textColor = [UIColor brownColor];
centerLabel.layer.borderColor = [UIColor whiteColor].CGColor;
centerLabel.layer.borderWidth = 0.5;
centerLabel.layer.cornerRadius = 2;
centerLabel.layer.masksToBounds = YES;
[centerLabel sizeToFit];
labelWidth += centerLabel.bounds.size.width + 6.0 + 4.0;
if (labelWidth >= [UIScreen mainScreen].bounds.size.width - 10.0 * 6) {
labelWidth = labelWidth - centerLabel.bounds.size.width - 6.0 - 4.0;
endIndex = i;
break ;
}
}
return @{[NSString stringWithFormat:@"%ld", endIndex] : [NSNumber numberWithFloat:([UIScreen mainScreen].bounds.size.width - labelWidth) / 2.0]};
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.