嗨,我想实现如上图所示的屏幕。数据以JSON字符串的形式来自服务器,我已经解析了数据。
代码如下:
UILabel *lblTitle = [self createDynamicLabel:@"Seroquel"
contentFrame:CGRectMake(10, 5, 150, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
[scrollview addSubview:lblTitle];
[lblTitle release];
UILabel *lblGeneric = [self createDynamicLabel:@"Generic Name:"
contentFrame:CGRectMake(10, lblTitle.frame.size.height+5, 130, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
[scrollview addSubview:lblGeneric];
[lblGeneric release];
UILabel *lblGenericName = [self createDynamicLabel:DrugInfo.GenName
contentFrame:CGRectMake(lblGeneric.frame.size.width+5, 24, 150, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
[scrollview addSubview:lblGenericName];
[lblGenericName release];
UILabel *lblManufacturer = [self createDynamicLabel:@"Manufacturer:"
contentFrame:CGRectMake(10, 44, 115, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
[scrollview addSubview:lblManufacturer];
[lblManufacturer release];
UILabel *lblManufacturerName = [self createDynamicLabel:DrugInfo.Mtr
contentFrame:CGRectMake(115, 43, 150, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
[scrollview addSubview:lblManufacturerName];
[lblManufacturerName release];
//[self createDynamicView:CGRectMake(10, 70, 153, 128)];
UILabel *lblCommon = [self createDynamicLabel:@"Common uses:"
contentFrame:CGRectMake(10, 209, 110, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
lblCommon.backgroundColor = [UIColor whiteColor];
[lblCommon sizeToFit];
[scrollview addSubview:lblCommon];
[lblCommon release];
NSString *genericInfo = [NSString stringWithFormat:@" %@",DrugInfo.ComUse];
UILabel *lblCommonUse = [self createDynamicLabel:genericInfo
contentFrame:CGRectMake(10, 208, 310, 80)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
//[lblCommonUse sizeToFit];
lblCommonUse.lineBreakMode = UILineBreakModeWordWrap;
lblCommonUse.numberOfLines = 0;
[scrollview addSubview:lblCommonUse];
[lblCommonUse release];
UILabel *lblBefore = [self createDynamicLabel:@" Before Using:"
contentFrame:CGRectMake(10, 294, 102, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
[lblBefore sizeToFit];
lblBefore.backgroundColor = [UIColor whiteColor];
[scrollview addSubview:lblBefore];
[lblBefore release];
genericInfo = [NSString stringWithFormat:@" %@",DrugInfo.BeforeUse];
UILabel *lblBeforeUse = [self createDynamicLabel:genericInfo
contentFrame:CGRectMake(10, 290, 308, 120)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
lblBeforeUse.lineBreakMode = UILineBreakModeWordWrap;
lblBeforeUse.numberOfLines = 0;
[scrollview addSubview:lblBeforeUse];
[lblBeforeUse release];
}
}
}
-(UILabel *)createDynamicLabel:(NSString *)setTitle
contentFrame:(CGRect)labelFrame
color:(UIColor *)labelColor
font:(UIFont *)labelFont {
UILabel *dynamicLabel = [[UILabel alloc] init];
dynamicLabel.frame = labelFrame;
dynamicLabel.text = setTitle;
dynamicLabel.textColor = labelColor;
dynamicLabel.font = labelFont;
dynamicLabel.backgroundColor = [UIColor clearColor];
return dynamicLabel;
}
但是如果数据超过可以在屏幕上显示的数据,它就会剪切它并且我想显示所有数据。任何人都可以帮我这么做吗?
提前致谢。
答案 0 :(得分:0)
首先,我可以看到你将UI控件添加到UIScrollView,如果是这种情况,你需要确保你正在设置UIScrollView的contentSize属性。 UIScrollView不知道内容有多大,所以你需要弄清楚它并告诉UIScrollView。
如果使用定位变量(即CGFloat top; CGFloat left;)为控件设置框架并在添加控件时更新它们,而不是将位置值硬编码到代码中,则可以非常轻松地实现这一点。像这样的东西
CGFloat top = 5;
CGFloat left = 10;
UILabel *lblTitle = [self createDynamicLabel:@"Seroquel"
contentFrame:CGRectMake(left, top, 150, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
[scrollview addSubview:lblTitle];
[lblTitle release];
top += lblTitle.frame.size.height;
UILabel *lblGeneric = [self createDynamicLabel:@"Generic Name:"
contentFrame:CGRectMake(left, top, 130, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
[scrollview addSubview:lblGeneric];
[lblGeneric release];
left += lblGeneric.frame.size.width;
UILabel *lblGenericName = [self createDynamicLabel:DrugInfo.GenName
contentFrame:CGRectMake(left, top, 150, 20)
color:[UIColor blackColor]
font:[UIFont fontWithName:@"Arial-BoldMT" size:17]];
[scrollview addSubview:lblGenericName];
[lblGenericName release];
然后,最后,您可以使用top
制作CGSize
来设置contentSize。
我还建议您查看使用UITableView或使用NSAttributedString,因为它们可能会帮助您以其他方式实现您想要的效果。