import UIKit
class Profile: UIViewController,UIScrollViewDelegate,UITextViewDelegate{
@IBOutlet weak var tx: UITextView!
@IBOutlet weak var sc: UIScrollView!
@IBOutlet weak var l1: UILabel!
@IBOutlet weak var l2: UILabel!
@IBOutlet weak var l3: UILabel!
@IBOutlet weak var l4: UILabel!
@IBOutlet weak var l5: UILabel!
@IBOutlet weak var l6: UILabel!
@IBOutlet weak var l7: UILabel!
@IBOutlet weak var l8: UILabel!
@IBOutlet weak var l9: UILabel!
@IBOutlet weak var l10: UILabel!
@IBOutlet weak var l11: UILabel!
@IBOutlet weak var l12: UILabel!
@IBOutlet weak var l13: UILabel!
@IBOutlet weak var l14: UILabel!
@IBOutlet weak var l15: UILabel!
@IBOutlet weak var l16: UILabel!
@IBOutlet weak var l17: UILabel!
@IBOutlet weak var l18: UILabel!
@IBOutlet weak var l19: UILabel!
@IBOutlet weak var l20: UILabel!
var toPass:String!
var toPass1: String!
var toPass2: String!
var toPass3: String!
var toPass4: String!
var toPass5: String!
var toPass6: String!
var toPass7: String!
var toPass8: String!
var toPass9: String!
var toPass10: String!
var toPass11: String!
var toPass12: String!
var toPass13: String!
var toPass14: String!
var toPass15: String!
var toPass16: String!
var toPass17: String!
var toPass18: String!
var toPass19: String!
var toPass20: String!
override func viewDidLoad() {
super.viewDidLoad()
l1.text = toPass
l2.text = toPass1
l3.text = toPass2
l4.text = toPass3
l5.text = toPass4
l6.text = toPass5
l7.text = toPass6
l8.text = toPass7
l9.text = toPass8
l10.text = toPass9
l11.text = toPass10
l12.text = toPass11
l13.text = toPass12
l14.text = toPass13
//l15.text = toPass14
l16.text = toPass15
l17.text = toPass16
l18.text = toPass17
l19.text = toPass13
l20.text = toPass20
tx.text = "\(toPass20)" + "\(toPass20)"
sc.minimumZoomScale = 1.0
sc.maximumZoomScale = 10
sc.delegate = self
}
func scrollViewWillBeginDragging(sc: UIScrollView)
{
NSLog("hello")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
我正在静态创建标签。我想动态创建,滚动时我需要从服务器获取数据并动态显示。
答案 0 :(得分:1)
您可以使用我的方法将动态高度设置为UILable ......
-(CGRect)calculateHeightOfTextFromWidth:(NSString*)resizableLable :(CGFloat)hightofLastLable
{
CGSize constrainedSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width , 9999);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"HelveticaNeue" size:11.0], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"textToShow" attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.width > [[UIScreen mainScreen] bounds].size.width )
{
requiredHeight = CGRectMake(0,hightofLastLable,[[UIScreen mainScreen] bounds].size.width , requiredHeight.size.height);
}
return requiredHeight;
}
添加UIlable和setFrame
UILabel *lblAddress = [[UILabel alloc]init];
[lblAddress setFrame:[self calculateHeightOfTextFromWidth:@"your Text" :lastLable.X+lastLable.Hight+5]];
lblAddress.text = @"your Text ";
lblAddress.numberOfLines = 0;
立即添加在滚动视图中添加Lable
float fscrview = lblAddress.frame.origin.y + lblAddress.frame.size.height + 20;
yourScrollView.contentSize=CGSizeMake([[UIScreen mainScreen] bounds].size.width , fscrview);
[yourScrollVieww addSubview:lblAddress];
希望帮助您解决问题..