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];
Related
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.
In one of my viewController, I have a scrollView, a UIView which contains and UIImageView, UITextView etc. for the scrollview here's the code what i wrote:
overViewScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0,screenFrame.size.width,screenFrame.size.height)];
[overViewScroll setCanCancelContentTouches:NO];
overViewScroll.pagingEnabled = YES;
overViewScroll.clipsToBounds = NO;
overViewScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[overViewScroll setContentSize:CGSizeMake(screenFrame.size.width, screenFrame.size.height-25)];
[overViewScroll setScrollEnabled:YES];
[overViewScroll setAlwaysBounceVertical:YES];
[overViewScroll setShowsVerticalScrollIndicator:YES];
UIView * completeView = [[UIView alloc]initWithFrame:CGRectMake(0,0,screenFrame.size.width,screenFrame.size.height)];
UILabel* heading1 = [[UILabel alloc]initWithFrame:CGRectMake(5,0,310,30)];
heading1.text = name;
heading1.backgroundColor = [UIColor clearColor];
UIFont * headingFont = [UIFont fontWithName:#"HelVetica" size:15];
heading1.font = headingFont;
[completeView addSubview:heading1];
//NSSet * imageString = [projectHeroData valueForKey:#"imageURL"];
NSString * urlString = #"";
NSURL * url = [NSURL URLWithString:urlString];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage * projImage = [[UIImage alloc]initWithData:imageData];
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(screenFrame.size.width/70,screenFrame.size.height/50,screenFrame.size.width-((screenFrame.size.width/70)*2),screenFrame.size.height/3)];
imgView.image = projImage;
imgView.backgroundColor = [UIColor blueColor];
[completeView addSubview:imgView];
UILabel * abstract = [[UILabel alloc]initWithFrame:CGRectMake(screenFrame.size.width/41,screenFrame.size.height/31 + screenFrame.size.height/3,screenFrame.size.width/4,screenFrame.size.height/30)];
abstract.text = #"Abstract-";
abstract.font = [UIFont boldSystemFontOfSize:fontSize];
abstract.backgroundColor = [UIColor clearColor];
[completeView addSubview:abstract];
UILabel * lastUpdated = [[UILabel alloc]initWithFrame:CGRectMake(screenFrame.size.width/41 + screenFrame.size.width/5,screenFrame.size.height/31 + screenFrame.size.height/3,screenFrame.size.width-screenFrame.size.width/4,screenFrame.size.height/30)];
NSString * string = modDate ;
NSString * updated = [NSString stringWithFormat:#"Last Updated-%#",string];
lastUpdated.text = updated;
lastUpdated.backgroundColor = [UIColor clearColor];
lastUpdated.font = [UIFont boldSystemFontOfSize:fontSize];
[completeView addSubview:lastUpdated];
UITextView * textView2 = [[UITextView alloc]initWithFrame:CGRectMake(screenFrame.size.width/70,(screenFrame.size.height/31 + screenFrame.size.height/3)+screenFrame.size.height/30,screenFrame.size.width-screenFrame.size.width/70*2,screenFrame.size.height)];
textView2.text = abst;
textView2.editable = NO;
textView2.font = [UIFont systemFontOfSize:fontSize];
textView2.backgroundColor = [UIColor clearColor];
[completeView addSubview:textView2];
[overViewScroll addSubview:completeView];
[self.view addSubview:overViewScroll];
everything is addded to the UIView and UIView is added to the UIScrollView.
But the problem is its behaving weird, image is not scrolling all the way through and the text in Textview is scrolling in itself.
Please help!!
change the contentsize of your scrollview.your UIview size and uiscrollview size is same if the UIScrollview contentsize is more than its subview size then scroll will happen change the line [overViewScroll setContentSize:CGSizeMake(screenFrame.size.width, screenFrame.size.height-25)]; like this [overViewScroll setContentSize:CGSizeMake(screenFrame.size.width, (screenFrame.size.height-25)*2)];.it will allow you to horizontal scroll And by default textview having scroll property its self.
overViewScroll =[[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,screenFrame.size.width,screenFrame.size.height)];
[overViewScroll setCanCancelContentTouches:NO];
overViewScroll.pagingEnabled = YES;
overViewScroll.clipsToBounds = NO;
overViewScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[overViewScroll setContentSize:CGSizeMake(320,600)];
[overViewScroll setScrollEnabled:YES];
[overViewScroll setAlwaysBounceVertical:YES];
[overViewScroll setShowsVerticalScrollIndicator:YES];
UIView * completeView = [[UIView alloc]initWithFrame:CGRectMake(0,0,screenFrame.size.width,screenFrame.size.height)];
UILabel* heading1 = [[UILabel alloc]initWithFrame:CGRectMake(5,0,310,30)];
heading1.text = name;
heading1.backgroundColor = [UIColor clearColor];
UIFont * headingFont = [UIFont fontWithName:#"HelVetica" size:15];
heading1.font = headingFont;
[completeView addSubview:heading1];
//NSSet * imageString = [projectHeroData valueForKey:#"imageURL"];
NSString * urlString = #"";
NSURL * url = [NSURL URLWithString:urlString];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage * projImage = [[UIImage alloc]initWithData:imageData];
UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(screenFrame.size.width/70,screenFrame.size.height/50,screenFrame.size.width-((screenFrame.size.width/70)*2),screenFrame.size.height/3)];
imgView.image = projImage;
imgView.backgroundColor = [UIColor blueColor];
[completeView addSubview:imgView];
UILabel * abstract = [[UILabel alloc]initWithFrame:CGRectMake(screenFrame.size.width/41,screenFrame.size.height/31 + screenFrame.size.height/3,screenFrame.size.width/4,screenFrame.size.height/30)];
abstract.text = #"Abstract-";
abstract.font = [UIFont boldSystemFontOfSize:fontSize];
abstract.backgroundColor = [UIColor clearColor];
[completeView addSubview:abstract];
UILabel * lastUpdated = [[UILabel alloc]initWithFrame:CGRectMake(screenFrame.size.width/41 + screenFrame.size.width/5,screenFrame.size.height/31 + screenFrame.size.height/3,screenFrame.size.width-screenFrame.size.width/4,screenFrame.size.height/30)];
NSString * string = modDate ;
NSString * updated = [NSString stringWithFormat:#"Last Updated-%#",string];
lastUpdated.text = updated;
lastUpdated.backgroundColor = [UIColor clearColor];
lastUpdated.font = [UIFont boldSystemFontOfSize:fontSize];
[completeView addSubview:lastUpdated];
UITextView * textView2 = [[UITextView alloc]initWithFrame:CGRectMake(screenFrame.size.width/70,(screenFrame.size.height/31 + screenFrame.size.height/3)+screenFrame.size.height/30,screenFrame.size.width-screenFrame.size.width/70*2,screenFrame.size.height)];
textView2.text = abst;
textView2.editable = NO;
textView2.font = [UIFont systemFontOfSize:fontSize];
textView2.backgroundColor = [UIColor clearColor];
[completeView addSubview:textView2];
[overViewScroll addSubview:completeView];
[self.view addSubview:overViewScroll];
try this it will work. another way is u yse interface builder and select scoll view and drag and drop other label image view on scroll view.
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];
}
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.
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.