I am parsing the XML file through URL and displaying the values as label dynamically.
If values are changed in xml file, the new values of labels are overlapped on old values
How should i fix it
In viewDidLoad
for(int i=0;i<[appDelegate.books count]; i++){
[self createLabel:labelname labelFrame:CGRectMake(LXCOOR, LYCOOR,LWIDTH,LHEIGHT)];
}
-(void) createLabel :(NSString * ) aTitle labelFrame:(CGRect) aFrame{
label1 =[[UILabel alloc] initWithFrame:aFrame];
//label.font=[UIFont fontWithName:#"Helvetica" size:TEXTSIZE];
label1.font=[UIFont systemFontOfSize: 10];
label1.textAlignment = UITextAlignmentLeft;
label1.text=[NSString stringWithString:aTitle];
label1.textColor=[UIColor blackColor];
label1.backgroundColor = [UIColor clearColor];
[sv addSubview:label1];
[self.view addSubview:sv];
}
remove the sv before adding it to the view...
-(void) createLabel :(NSString * ) aTitle labelFrame:(CGRect) aFrame{
if ([[sv subviews] count] > 0)
{
for(int i=0 ; i< [[sv subviews] count] ; i++)
{
UIView *subView = [[sv subviews] objectAtIndex:0];
[subView removeFromSuperview];
i++;
}
}
[sv removeFromSuperView];
label1 =[[UILabel alloc] initWithFrame:aFrame];
//label.font=[UIFont fontWithName:#"Helvetica" size:TEXTSIZE];
label1.font=[UIFont systemFontOfSize: 10];
label1.textAlignment = UITextAlignmentLeft;
label1.text=[NSString stringWithString:aTitle];
label1.textColor=[UIColor blackColor];
label1.backgroundColor = [UIColor clearColor];
[sv addSubview:label1];
[self.view addSubview:sv];
}
In viewDidLoad add,
[self.view addSubview:sv];
Then modify the "createLabel" function to
-(void) createLabel :(NSString * ) aTitle labelFrame:(CGRect) aFrame{
for(UIView *tempView in [sv subviews]) {
[subView removeFromSuperview];
}
label1 =[[UILabel alloc] initWithFrame:aFrame];
//label.font=[UIFont fontWithName:#"Helvetica" size:TEXTSIZE];
label1.font=[UIFont systemFontOfSize: 10];
label1.textAlignment = UITextAlignmentLeft;
label1.text=[NSString stringWithString:aTitle];
label1.textColor=[UIColor blackColor];
label1.backgroundColor = [UIColor clearColor];
[sv addSubview:label1];
}
as mentioned above, you shouldn't recreate the label over and over. Instead you should create one instance of it in viewDidLoad (or something that loads once)
-(void)viewDidLoad { ...
//code
CGRect aFrame = CGRectMake(x, y, width, height);
label1 =[[UILabel alloc] initWithFrame:aFrame];
[sv addSubview:label1];
[self.view addSubview:sv];
}
next you should call createLabel where you want to, for instance a button click or a scroll gesture. This should be your new createLabel without adding a subview
-(void) createLabel :(NSString * ) aTitle labelFrame:(CGRect) aFrame{
label1.font=[UIFont systemFontOfSize: 10];
label1.textAlignment = UITextAlignmentLeft;
label1.text=[NSString stringWithString:aTitle];
label1.textColor=[UIColor blackColor];
label1.backgroundColor = [UIColor clearColor];
}
Related
I tried to set the right side margin in UItextfield but I can't get success for this.
I tried below code:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, 60, 25) ];
[lbl setText:#"Partenza:"];
[lbl setFont:[UIFont fontWithName:#"Verdana" size:12]];
[lbl setTextColor:[UIColor grayColor]];
[lbl setTextAlignment:UITextAlignmentLeft];
txtFCarat.rightView = lbl;
txtFCarat.rightViewMode = UITextFieldViewModeAlways;
txtFCarat.delegate = self;
Please help me if any body have a solution for this!
You can override this below two methods
#implementation rightTextField
// placeholder position
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset( self.bounds , margin position , margin position );
}
// text position
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset( self.bounds , margin position ,margin position );
}
Edit: use to bind code like
textField.bounds = [self editingRectForBounds:textField.bounds];
textField.bounds = [self textRectForBounds:textField.bounds];
Your expected Answer:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon.png"]];
textfield.rightView = imageView;
textfield.rightViewMode = UITextFieldViewModeAlways;
Add the below line where you are creating textfield..and set value as per requirement
textField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 000, 0);
Another way to achieve this by overriding the textRectForBounds
Let me to give complete Description how to do this or achieve it
First you create a class and name "textField"(or what ever you Want) and must be sure that IT IS SUBCLASS OF UITextField.
2.Now open the textfield.m class and use this code or paste below code in this class (textfield):
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 50, bounds.origin.y,
bounds.size.width - 20, bounds.size.height);
}
-(CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
If you want to customize the textfield more then you can use here only or in calling Class like
use the below code :Note it is only for testing purpose.it depend on your requirement
- (void)settingTextField {
[self setTextField];
[self setKeyboardAppearance:UIKeyboardAppearanceAlert];
}
- (void)setTextField {
[self setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[self setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[self setTextAlignment:NSTextAlignmentCenter];
[self setValue:[UIColor colorWithRed:120.0/225.0 green:120.0/225.0 blue:120.0/225.0 alpha:1.0] forKeyPath:#"_placeholderLabel.textColor"];
[self setFont:[UIFont boldSystemFontOfSize:15.0f]];
}
3.improt textField.h class into your view-controller where you wants to create textfield
and you call it like this.
#import "textField.h"
//Viewcontroller Class
- (void)viewDidLoad
{
[super viewDidLoad];
textField *field1 = [[textField alloc] initWithFrame:CGRectMake(50, 50, 200, 20)];
field1.backgroundColor=[UIColor greenColor];
field1.placeholder=#"0";
[self.view addSubview:field1];
}
Try to adjust the value textRectForBounds as per you reqitement
NSString *myString = #"Partenza:";
UIFont *myFont = [UIFont fontWithName:#"Verdana" size:12];
CGSize myStringSize = [myString sizeWithFont:myFont];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, myStringSize.width + 10, 25) ];
[lbl setText:myString];
[lbl setFont:[UIFont fontWithName:#"Verdana" size:12]];
[lbl setTextColor:[UIColor grayColor]];
[lbl setTextAlignment:UITextAlignmentLeft];
txtFCarat.rightView = lbl;
txtFCarat.rightViewMode = UITextFieldViewModeAlways;
I have created more than one label using this code,
.H file
#interface ViewController : UIViewController
{
NSArray * phraseAry ;
UIView * containerView;
UILabel * oldLabel;
NSMutableArray *dataArray;
}
#property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;
.M file
- (void)viewDidLoad
{
[super viewDidLoad];
heightValue = 20;
widthValue = 0;
xValue = 5;
yValue = 10;
containerView = [[UIView alloc] init];
for (int i=0; i<phraseAry.count; i++) {
widthValue = [self returnWidth:[phraseAry objectAtIndex:i]];
int newXValue = xValue+widthValue+5;
//NSLog(#"newXValue : %i",newXValue);
if (newXValue > 310) {
yValue +=20;
xValue = 5;
newXValue = xValue+widthValue+5;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(xValue, yValue, widthValue, heightValue)];
lbl.text = [phraseAry objectAtIndex:i];
[lbl setFont:[UIFont fontWithName:#"Helvetica" size:14.0]];
lbl.tag = i;
lbl.textColor = [UIColor colorWithRed:(92/255.0) green:(109/255.0) blue:(43/255.0) alpha:1];
[containerView addSubview:lbl];
xValue = newXValue;
} else {
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(xValue, yValue, widthValue, heightValue)];
lbl.text = [phraseAry objectAtIndex:i];
[lbl setFont:[UIFont fontWithName:#"Helvetica" size:14.0]];
lbl.tag = i;
lbl.textColor = [UIColor colorWithRed:(92/255.0) green:(109/255.0) blue:(43/255.0) alpha:1];
[containerView addSubview:lbl];
xValue = newXValue;
}
}
containerView.frame = CGRectMake(0, 0, 320, yValue);
//add code to customize, e.g. polygonView.backgroundColor = [UIColor blackColor];
[self.myScrollView addSubview:containerView];
self.myScrollView.contentSize = containerView.frame.size;
}
And than i set background color and textcolor in specific table using this code
- (void)updateLabelMethod
{
oldLabel.backgroundColor = [UIColor clearColor];
UILabel *label = (UILabel *)[containerView viewWithTag:2];
oldLabel = label;
label.backgroundColor = [UIColor colorWithRed:(98/255.0) green:(147/255.0) blue:(216/255.0) alpha:1];
label.textColor = [UIColor whiteColor];
containerView.backgroundColor = [UIColor clearColor];
}
It will update background of label fine but when i use this code to update textcolor it will display error like this
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTextColor:]: unrecognized selector sent to instance 0xbaa3b30'
Any ways to set textcolor? Plz help.
Start your label tag from 1 instead of 0, because by default the tag value is 0 for all view.
lbl.tag = i+1;
Because
UILabel *label = (UILabel *)[containerView viewWithTag:0];
it will return containerView itself, and UIView not have textColor property :P
[containerView viewWithTag:2];
check container view has only one view that is a label with tag 2.
May be some other view is set with tag 2 and may be causing the issue.
NSLog(#"%#",label);
can give you what the label here gives the output .It should point out an UILabel itself
make use of isKindOfClass: to identify it is a label you are updating
for (UILable *lblTemp in containerView.subviews){
if ([lblTemp isKindOfClass:[UILable class]] && lblTemp.tag==2){
// change the lbl color
break;
}
}
check using this code ;)
How do I make UINavigationBar title to be user editable, I've tried setting the titleView to be a textfield however it doesn't look the same.. It's missing that outline or drop shadow. I'm aiming for it to look like the default one.
This is what i'm implementing at the moment:
_titleField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 26)];
[_titleField setDelegate:self];
_titleField.text = _bugDoc.data.title;
_titleField.font = [UIFont boldSystemFontOfSize:20];
_titleField.textColor = [UIColor whiteColor];
_titleField.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = _titleField;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadTitle];
}
- (void)loadTitle
{
txtField_navTitle = [[UITextField alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x,7,self.view.bounds.size.width,31)];
txtField_navTitle.delegate = self;
[txtField_navTitle setBackgroundColor:[UIColor clearColor]];
txtField_navTitle.textColor = [UIColor whiteColor];
txtField_navTitle.textAlignment = UITextAlignmentCenter;
txtField_navTitle.borderStyle = UITextBorderStyleNone;
txtField_navTitle.font = [UIFont boldSystemFontOfSize:20];
txtField_navTitle.autocorrectionType = UITextAutocorrectionTypeNo;
txtField_navTitle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.navigationItem setTitleView:txtField_navTitle];
txtField_navTitle.layer.masksToBounds = NO;
txtField_navTitle.layer.shadowColor = [UIColor whiteColor].CGColor;
txtField_navTitle.layer.shadowOpacity = 0;
[txtField_navTitle release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.title = textField.text;
return YES;
}
Please dont forget to #import <QuartzCore/QuartzCore.h>
Try this way this works for me.
// Custom Navigation Title.
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.title;
tlabel.textAlignment=NSTextAlignmentCenter;
tlabel.font = [UIFont boldSystemFontOfSize:17.0];
tlabel.textColor=[UIColor colorWithRed:7.0/255.0 green:26.0/255.0 blue:66.0/255.0 alpha:1.0];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;
I am new to iPhone,
How do I vertically align my text in the UILabel?
Here is my Code snippet,
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,100,100);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
[scrollVw addSubview:lbl];
text displayed is in the format of 123456 but i want text should be display vertically like,
6
5
4
3
2
1
Any help will be appriciated.
Its impossible to align the text in UILabel vertically. But, you can dynamically change the height of the label using sizeWithFont: method of NSString, and just set its x and y as you want.
As an alternative you can use UITextField. It supports the contentVerticalAlignment peoperty as it is a subclass of UIControl. You have to set its userInteractionEnabled to NO to prevent user from typing text on it.
EDIT 1
Well instead of a UILabel , Make a UITableView like :-
TableActivityLevel=[[UITableView alloc] initWithFrame:CGRectMake(224, 203, 27, 0) style:UITableViewStylePlain];
TableActivityLevel.delegate=self;
TableActivityLevel.dataSource=self;
TableActivityLevel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
TableActivityLevel.rowHeight = 17;
TableActivityLevel.backgroundColor=[UIColor clearColor];
TableActivityLevel.layer.borderColor = [[UIColor clearColor]CGColor];
TableActivityLevel.separatorStyle = UITableViewCellSeparatorStyleNone;
EDIT 2 Found the method using UILabels too !! :) Check this out....
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 10.0, 100.0)];
[label1 setBackgroundColor:[UIColor clearColor]];
[label1 setNumberOfLines:0];
[label1 setCenter:self.view.center];
[label1 setFont:[UIFont fontWithName:#"Helvetica" size:12.0]];
[label1 setTextColor:[UIColor whiteColor]];
[label1 setTextAlignment:UITextAlignmentCenter];
[label1 setText:#"1 2 3 4 5 6"];
[self.view addSubview:label1];
If it just a single lined text as in your example you can use various workarounds. I personally would create a UILabel subclass as follows,
#implementation VerticalLabel
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.numberOfLines = 0;
}
return self;
}
- (void)setText:(NSString *)text {
NSMutableString *newString = [NSMutableString string];
for (int i = text.length-1; i >= 0; i--) {
[newString appendFormat:#"%c\n", [text characterAtIndex:i]];
}
super.text = newString;
}
#end
You now just have to replace
UILabel *lbl = [[UILabel alloc] init];
with
UILabel *lbl = [[VerticalLabel alloc] init];
to get vertical text.
Hi the following code is work well
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,10,300);
lbl.backgroundColor=[UIColor clearColor];
lbl.numberOfLines=6; // Use one to 6 numbers
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
NSString *reverse=lbl.text;
NSMutableString *reversedString = [NSMutableString string];
NSInteger charIndex = [reverse length];
while (charIndex > 0) {
charIndex--;
NSRange subStrRange = NSMakeRange(charIndex, 1);
[reversedString appendString:[reverse substringWithRange:subStrRange]];
}
NSLog(#"%#", reversedString);
CGSize labelSize = [lbl.text sizeWithFont:lbl.font
constrainedToSize:lbl.frame.size
lineBreakMode:lbl.lineBreakMode];
lbl.frame = CGRectMake(
lbl.frame.origin.x, lbl.frame.origin.y,
lbl.frame.size.width, labelSize.height);
lbl.text=reversedString;
[scrollview addSubview:lbl];
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,10,100);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"123456";
lbl.numberOfLines = 10;
[self.view addSubview:lbl];
You cant do vertical allignment but use another way to display it
UILabel *lbl=[[UILabel alloc]init];
lbl.frame=CGRectMake(10,10,100,400);
lbl.backgroundColor=[UIColor clearColor];
lbl.font=[UIFont systemFontOfSize:13];
lbl.text=#"1\n2\n3\n4\n5\n6";
[scrollVw addSubview:lbl];
How does one create a dynamic thumbnail grid?
How is a grid created with thumbnails of images like the photo app on iphone?
How are these spaces arranged dynamically? e.g. adding and removing thumbnails.
this is my simple grid view app
- (void)viewDidAppear:(BOOL)animated {
[[Grid_ViewAppDelegate sharedAppDelegate] hideLoadingView];
temp = temp+1;
if (temp ==1){
NSLog(#"temp:%d",temp);
int n = 20; //Numbers of array;
NSArray *t = [[NSArray alloc] initWithObjects:#"calpie.gif",#"chrysler_electric.png",#"chrysler_fiat_cap.gif",#"cleanup.gif",#"ecylindri.gif",#"elect08.png",#"globe.gif",#"heat.gif",#"insur.gif"
,#"jobs.gif",#"office.gif",#"opec1.gif",#"orng_cap.gif",#"poll.gif",#"pollen.gif",#"purbar.gif",#"robinson.gif",#"robslink_cap.gif",#"scot_cap.gif",#"shoes.gif",nil];
myScrollView.contentSize = CGSizeMake(320, 460+n*2);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 1;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
NSString *mxiURL = #"http://meta-x.com/biflash/images/";
int i =0,i1=0;
while(i<n){
int yy = 4 +i1*79;
for(int j=0; j<4;j++){
if (i>=n) break;
CGRect rect;
rect = CGRectMake(4+79*j, yy, 75, 75);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
NSString *nn = [mxiURL stringByAppendingString:[t objectAtIndex:i]];
UIImage *buttonImageNormal=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: nn]]];
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
button.tag =i;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
//[self.view addSubview:button];
[myScrollView addSubview:button];
//[buttonImageNormal release];
[button release];
i++;
//
}
i1 = i1+1;
//i=i+4;
}
}
}
in AppDelegate
+ (Grid_ViewAppDelegate *)sharedAppDelegate
{
return (Grid_ViewAppDelegate *)[UIApplication sharedApplication].delegate;
}
- (void)showLoadingView
{
if (loadingView == nil)
{
loadingView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
loadingView.opaque = NO;
loadingView.backgroundColor = [UIColor grayColor];
loadingView.alpha = 0.5;
UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(142.0, 222.0, 37.0, 37.0)];
[spinningWheel startAnimating];
spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loadingView addSubview:spinningWheel];
[spinningWheel release];
CGRect label = CGRectMake(142.0f, 255.0f, 71.0f, 20.0f);
lblLoading = [[UILabel alloc] initWithFrame:label];
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont boldSystemFontOfSize:14];
// UILabel
lblLoading.backgroundColor =[UIColor clearColor];
[lblLoading setText:#"Loading..."];
//[lblLoading setTextAlignment:UITextAlignmentCenter];
[loadingView addSubview:lblLoading];
}
[window addSubview:loadingView];
}
- (void)hideLoadingView
{
[loadingView removeFromSuperview];
}
definitely if u apply this logic. u get succeed