Content IN UILABEL is Overlapping - iphone

HI,
I am using the following code for displaying data dynamically on a label.ITs working fine.But sometimes the content is overlapping with the subhead title and description.Please help me in this.I am not able to trace out this.THANKS IN ADVANCE
for (int i=0; i<[article.subheadArray count]; i++) {
//subhead Title
subheadTitle = [[UILabel alloc] initWithFrame:CGRectMake(xoffset, yoffset+total, 320-xoffset, 300+lblheight)];
subheadTitle.backgroundColor=[UIColor clearColor];
//subheadTitle.textColor=[UIColor blueColor];
[subheadTitle setFont: [UIFont fontWithName:#"Arial-BoldMT" size:17]];
subheadTitle.textColor = [UIColor colorWithRed:45.0/255.0 green:99.0/255.0 blue:03.0/255.0 alpha:1];
subHead = [article.subheadArray objectAtIndex:i];
expectedLabelSize = [subHead.Title sizeWithFont:subheadTitle.font
constrainedToSize:maximumLabelSize
lineBreakMode:subheadTitle.lineBreakMode];
newFrame = subheadTitle.frame;
newFrame.size.height=expectedLabelSize.height;
subheadTitle.frame=newFrame;
total+=expectedLabelSize.height+yoffset;
NSLog(#"%# %d",subHead.Title,total);
subheadTitle.numberOfLines=0;
subheadTitle.text=subHead.Title;
[scrollView addSubview:subheadTitle];
//subhead Description
subheadlabel = [[UILabel alloc] initWithFrame:CGRectMake(xoffset, yoffset+total, 320-xoffset, 300+lblheight)];
subheadlabel.backgroundColor=[UIColor clearColor];
subHead = [article.subheadArray objectAtIndex:i];
//subheadlabel.font = [UIFont italicSystemFontOfSize:fontsize];
[subheadlabel setFont: [UIFont fontWithName:#"ArialMT" size:fontsize]];
expectedLabelSize=[subHead.Description sizeWithFont:subheadlabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:subheadlabel.lineBreakMode];
newFrame = subheadlabel.frame;
newFrame.size.height=expectedLabelSize.height;
subheadlabel.frame=newFrame;
total+=expectedLabelSize.height+yoffset;
subheadlabel.text = subHead.Description;
NSLog(#"subhead desc: %d %d",total,subheadlabel.numberOfLines);
subheadlabel.numberOfLines =0;
[subheadlabel sizeToFit];
[scrollView addSubview:subheadlabel];
//Adding subhead label to subheadlabelarr
[subheadlabelarr addObject:subheadTitle];
//subhead Image
for (int j=0; j<[subHead.imageArray count]; j++) {
Image* image = [subHead.imageArray objectAtIndex:j];
UIImage* uiImage = [XMLParsing getImagewithName:image.Name];
CGSize imageSize = uiImage.size;
if(imageSize.width>=300){
width = 300;
}
else {
width = imageSize.width;
}
if(imageSize.height>=200){
height = 200;
}else {
height = imageSize.height;
}
imageView = [[UIImageView alloc] initWithFrame:CGRectMake((320-width)/2,yoffset+total,width,height)];
//imageView = [[UIImageView alloc] initWithFrame:CGRectMake(200, yoffset+total, width, height)];
imageView.clipsToBounds = YES;
total+=imageView.frame.size.height+yoffset;
//************cell.myImageView.image=[XMLParser getImagewithName:#"og_logo.png"];
imageView.image = [XMLParsing getImagewithName:image.Name];
[scrollView addSubview:imageView];
}
}

I think both subheadTitle and subheadlabel will overlaps because of their frame. Can you check the values of expectedLabelSize in both cases.

Related

how to show the values in label which is in array index in iphone

I have array with two items i want that if i=0 then it may show Ali and 1 then Jawaad but i do not want it static i want dynamic if there are 100 items in array so they must be given according to their index to label i am using loop but this loop always sets Jawaad to label.
NSArray*myArray = [[NSArray alloc] initWithObjects:#"Ali",#"Jawaad",nil];
int countTest=[myArray count];
NSLog(#"count Test is %d",countTest);
for (int i=0; i<countTest; i++) {
DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(50,20,206,84)];
DescriptionLabel.textAlignment = UITextAlignmentLeft;
DescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
DescriptionLabel.numberOfLines = 0;
DescriptionLabel.textColor = [UIColor blackColor];
NSString*testting=[myArray objectAtIndex:i];
DescriptionLabel.text=testting;
DescriptionLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
[scrollView addSubview:DescriptionLabel];
}
Labels are overlapping. Try
int y = 20;
for (int i=0; i<countTest; i++) {
DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(50,y,206,84)];
y = y+ 84;
DescriptionLabel.textAlignment = UITextAlignmentLeft;
DescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
DescriptionLabel.numberOfLines = 0;
DescriptionLabel.textColor = [UIColor blackColor];
NSString*testting=[myArray objectAtIndex:i];
DescriptionLabel.text=testting;
DescriptionLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
[scrollView addSubview:DescriptionLabel];
}
Change :
DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(50,20,206,84)];
To :
DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(i*206,20,206,84)];
Add :
[scrollView addSubview:DescriptionLabel];
[DescriptionLabel release];
your code will overlap UILabel one upon one continuously because their frames are same.
You should increase y of the frame dynamically.
int y = 10;
for (int i=0; i<countTest; i++) {
DescriptionLabel = [[UILabel alloc ] initWithFrame:CGRectMake(50,y,206,84)];
DescriptionLabel.textAlignment = UITextAlignmentLeft;
DescriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
DescriptionLabel.numberOfLines = 0;
DescriptionLabel.textColor = [UIColor blackColor];
NSString*testting=[myArray objectAtIndex:i];
DescriptionLabel.text=testting;
DescriptionLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
[scrollView addSubview:DescriptionLabel];
y = y+30
}

a little uitextview position

i am writing this code to load a plist into 2 textview but i want them to adjust their height according to content size which i have done but i want the second uitextview to start from where the first one ends .. please help
for (int i = 0; i < arrresult.count; i++) {
NSArray *sorteddetail = [self.arrresult sortedArrayUsingFunction:sortint context:NULL];
DuaObject *duaobj2 = [[DuaObject alloc] initWithFilePath:[sorteddetail objectAtIndex:i]];
[duaobj2 loadFile];
CGRect frame;
frame.origin.x = self.scrollmain.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollmain.frame.size;
UITextView *subview = [[UITextView alloc] initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+10, 230, 120)];
UITextView *subview2 = [[UITextView alloc]initWithFrame:CGRectMake((frame.origin.x)+40,(frame.origin.y)+160,230, 120)];
subview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"BG_text.png"]];
subview2.backgroundColor = [UIColor whiteColor];
[subview setFont:[UIFont boldSystemFontOfSize:14]];
[subview2 setFont:[UIFont boldSystemFontOfSize:14]];
subview.layer.cornerRadius = 10 ;
subview2.layer.cornerRadius = 10;
NSString *p = [duaobj2 dua_arabic];
NSString *b = [duaobj2 eng_trans];
subview.text = p;
subview2.text = b;
[self.scrollmain addSubview:subview];
[self.scrollmain addSubview:subview2];
subview.editable = NO;
subview2.editable = NO;
subview.textColor = [UIColor blackColor];
subview2.textColor = [UIColor blackColor];
subview.backgroundColor = [UIColor whiteColor];
subview2.backgroundColor = [UIColor whiteColor];
scrollmain.showsHorizontalScrollIndicator = NO;
CGRect frame1 = subview.frame;
CGRect frame2 = subview2.frame;
frame1.size.height = subview.contentSize.height;
frame2.size.height = subview2.contentSize.height;
subview.frame = frame1;
subview2.frame = frame2;
}
You can use two UIWebView for this. After fetching text data from plist ,redirect it on particular web view. and use [webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"] method to get height in pixel. According to height of pixel you can re-adjust web views frame.
Edit:
Look this one : How do I size a UITextView to its content?
It will help you.

Displaying parse string as label issue

I've got a label which is displayed in a subview. I got the text of the label from a NSString on parse. However, I tried to display the text but it doesnt seems to work. There is no text displayed.
Here is my code. Please let me know if you know how to solve this:
PFQuery *query = [PFQuery queryWithClassName:#"myapp"];
[query getObjectInBackgroundWithId:#"myId"
block:^(PFObject *textu, NSError *error) {
if (!error) {
CGRect infoLabelRect = CGRectMake(10, 250, 260, 350);
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
NSString *text = [textu objectForKey:#"newstext"];
UIFont *font = nil;
CGFloat points = 17;
CGFloat maxHeight = infoLabel.frame.size.height;
CGFloat textHeight;
do {
font = [UIFont systemFontOfSize:points];
CGSize size = CGSizeMake(infoLabelRect.size.width, 100000);
CGSize textSize = [text sizeWithFont:font constrainedToSize:size lineBreakMode: NSLineBreakByWordWrapping];
textHeight = textSize.height;
points -= 1;
} while (textHeight > maxHeight);
infoLabel.font = font;
infoLabel.numberOfLines = 9;
infoLabel.textColor = [UIColor whiteColor];
infoLabel.textAlignment = NSTextAlignmentCenter;
infoLabel.backgroundColor = [UIColor clearColor];
infoLabel.shadowColor = [UIColor blackColor];
infoLabel.shadowOffset = CGSizeMake(0, 1);
[infoLabel sizeToFit];
[contentView addSubview:infoLabel];
} else {
// Log details of our failure
CGRect infoLabelRect = CGRectMake(10, 250, 260, 350);
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
infoLabel.text = #"Connection error!";
}
}];
Your first part of code doesn't contain assignment to text
infoLabel.text = text; // or #"Some text" for testing
Your last part of code only creates UILabel, but doesn't add it as subview
UILabel *infoLabel = [[UILabel alloc] initWithFrame:infoLabelRect];
infoLabel.text = #"Connection error!";
[contentView addSubview:infoLabel];

uiimageview gets nil and hence code crashes

in this block of code my uiimageview always get nil.as a result the code crashes.i can t figure out where is the uiimageview getting nil.in this code self refers to uitableviewcell.
the code crashes in the code where i am inserting the imageview to the array.is this a problem of memory retain?
MyAppDelegate *appDelegate =(myAppDelegate *)[[UIApplication sharedApplication]delegate];
UIImageView *profileImageView;
UILabel *tweetLabel;
UILabel *timeLabel;
UILabel *profileNameLabel;
UIView *message;
if(self==nil)
{
profileImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
profileImageView.backgroundColor= [UIColor clearColor];
profileImageView.tag = 1;
tweetLabel = [[UILabel alloc] initWithFrame:CGRectZero];
tweetLabel.backgroundColor = [UIColor clearColor];
tweetLabel.tag = 2;
tweetLabel.numberOfLines = 3;
tweetLabel.lineBreakMode = UILineBreakModeWordWrap;
tweetLabel.font = [UIFont systemFontOfSize:10.0];
tweetLabel.textColor=[UIColor blackColor];
timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
timeLabel.backgroundColor = [UIColor clearColor];
timeLabel.tag = 3;
timeLabel.numberOfLines = 1;
timeLabel.lineBreakMode = UILineBreakModeWordWrap;
timeLabel.font = [UIFont systemFontOfSize:12.0];
timeLabel.textColor=[UIColor blackColor];
profileNameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
profileNameLabel.backgroundColor = [UIColor clearColor];
profileNameLabel.tag = 4;
profileNameLabel.numberOfLines = 1;
profileNameLabel.lineBreakMode = UILineBreakModeWordWrap;
profileNameLabel.font = [UIFont boldSystemFontOfSize:14.0];
profileNameLabel.textColor=[UIColor blackColor];
message = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.frame.size.width, self.frame.size.height)];
message.tag = 0;
[message addSubview:profileImageView];
[message addSubview:tweetLabel];
[message addSubview:timeLabel];
[message addSubview:profileNameLabel];
[self addSubview:message];
}
else{
tweetLabel = (UILabel *)[[self.contentView viewWithTag:0]viewWithTag:2];
timeLabel = (UILabel *)[[self.contentView viewWithTag:0]viewWithTag:3];
profileNameLabel = (UILabel *)[[self.contentView viewWithTag:0]viewWithTag:4];
profileImageView = (UIImageView *)[self.contentView viewWithTag:1];
NSLog(#" profileImageView %#",profileImageView);
}
NSString *tweet=[tweetObject tweet];
NSString *profileName=[tweetObject author];
NSString *time=[tweetObject time];
CGSize textSize = [tweet sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];
CGSize timeSize = [time sizeWithFont:[UIFont systemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];
CGSize profileNameSize = [profileName sizeWithFont:[UIFont boldSystemFontOfSize:14.0] constrainedToSize:CGSizeMake(240.0f, 480.0f) lineBreakMode:UILineBreakModeWordWrap];
profileImageView.frame = CGRectMake(5,self.frame.size.height/2, 44, 44);
NSLog(#" profileImageView %#",profileImageView);
tweetLabel.frame = CGRectMake(54, profileImageView.frame.size.width, 240,25);
timeLabel.frame = CGRectMake(180, 5.0f, 100, timeSize.height);
profileNameLabel.frame = CGRectMake(54,5, 150, profileNameSize.height);
tweetLabel.text=tweet;
timeLabel.text=time;
profileNameLabel.text=profileName;
profileImageView.image = [[UIImage imageNamed:#"fbdefaultProfile.gif"]retain];
NSLog(#" myArray WILL FILL WITH THIS ELEMNTS %# %# %# ",profileImageView,[tweetObject imageURL],#"fbdefaultProfile.gif");
NSArray *myArray = [NSArray arrayWithObjects:profileImageView,[tweetObject imageURL],#"fbdefaultProfile.gif",nil];
NSLog(#" myArray %#",myArray);
NSLog(#"%# myArray ",myArray);
[appDelegate performSelectorInBackground:#selector(updateImageViewInBackground:) withObject:myArray];
}
return;
if(self==nil)
{
what is this.. I think it should be
if(self!=nil)
{
and if it is fine then why are you adding subview to a nill
if(self==nil)
{
//your code .....
//and then you are adding sub view to a nil which is useless
[self addSubview:message];
}
The issue could be in the below line of code.
profileImageView = (UIImageView *)[self.contentView viewWithTag:1];
May be you don't have the an view with tag id 1;
plz check against nil after getting the view from viewWithTag.
if([self.contentView viewWithTag:1] != nil)
{
profileImageView = (UIImageView *)[self.contentView viewWithTag:1];
}

increasing width of uilabel dynamically

I want to dynamically assign width to a label depending upon the text length to be displayed. The labels are it self being added on uiview. I am using following code but still i am getting label with shorter width.
- (id)initWithFrame:(CGRect)frame OrangeText:(NSString*)orange WhiteText:(NSString*)white {
if ((self = [super initWithFrame:frame])) {
CGSize textSize = [orange sizeWithFont:[UIFont systemFontOfSize:14]];
OrangeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, textSize.width, textSize.height+2)];
OrangeLabel.text = orange;
OrangeLabel.backgroundColor = [UIColor clearColor];
OrangeLabel.textColor = [UIColor orangeColor];
[self addSubview:OrangeLabel];
WhiteLabel = [[UILabel alloc] init];
CGSize whiteTextSize = [white sizeWithFont:[UIFont systemFontOfSize:14]];
WhiteLabel.frame = CGRectMake(OrangeLabel.frame.size.width+35, 5, whiteTextSize.width, whiteTextSize.height);
WhiteLabel.text = white;
WhiteLabel.backgroundColor = [UIColor clearColor];
WhiteLabel.textColor = [UIColor whiteColor];
[self addSubview:WhiteLabel]; // Initialization code
}
return self;
}
I think you are looking for this method
[myLabel sizeToFit];
This should resize the label frame to fit its contents.