want to create a variable, increment it and get new value - iphone

i'm creating a bunch of textfields. i am stacking them vertical, so i use:
CGRectMake(193, ((i * 45) + 45), 240, 30)
then after each textbox is created, i increment i.
int i = 0
CGRect myRect = CGRectMake(193, ((i * 45) + 45), 240, 30)
UITextField *myTextField01 = [[UITextField alloc] initWithFrame:myRect];
//format and add to view text field
i++;
//create next text field
myRect = CGRectMake(193, ((i * 45) + 45), 240, 30) //can i get rid of this line?
UITextField *myTextField01 = [[UITextField alloc] initWithFrame:myRect];
//format and add to view text field
i++;
i need to reassign the myRect to get i updated to the value of 1.
is there a better way to do this so i don't need to reassign myRect to get the update value of i?

CGRect is a struct, so you can increment the internal fields directly:
CGRect myRect = CGRectMake(193, 45, 240, 30);
UITextField *myTextField01 = [[UITextField alloc] initWithFrame:myRect];
myRect.origin.y += 45;
UITextField *myTextField02 = [[UITextField alloc] initWithFrame:myRect];
myRect.origin.y += 45;
UITextField *myTextField03 = [[UITextField alloc] initWithFrame:myRect];
I would also use a loop and some kind of container (probably NSArray), but that's a side topic ;)

Use myRect = CGRectOffset(myRect, 0, 45); each time. "i" is not used here because you offset the already offset rectangle.

Related

How to add space at start of UITextField

I am creating an app that contain multiple UITextField. In the textField i have sated border type to none and background image. It display fine, But now my text is started from the lest border which does not look good, like this
How can i add space at the start of the textfield?
Try this
UILabel * leftView = [[UILabel alloc] initWithFrame:CGRectMake(10,0,7,26)];
leftView.backgroundColor = [UIColor clearColor];
textField.leftView = leftView;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Here's #Kalpesh's answer in Swift 3.x:
let leftView = UILabel(frame: CGRectMake(10, 0.0, 7, 26))
leftView.backgroundColor = .clearColor()
customField.leftView = leftView
customField.leftViewMode = .Always
customField.contentVerticalAlignment = .Center
Swift 4.x
let leftView = UILabel(frame: CGRect(x: 10, y: 0, width: 7, height: 26))
leftView.backgroundColor =.clear
customField.leftView = leftView
customField.leftViewMode = .always
customField.contentVerticalAlignment = .center
You should subclass UITextField and override drawText function.
check this for help
OR
You can do following:
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, height_of_textfiled)];
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
Objective c
For padding only placeholder this code will work
usernameTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:#" Your text"];
For padding both placeholder and text of the UITextField this below code will work
usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30);
Swift 3.0
For padding only placeholder this code will work
yourTextField.attributedPlaceholder = NSAttributedString(string: " YourText")
For padding both placeholder and text of the UITextField this below code will work
usernameTF.layer.sublayerTransform = CATransform3DMakeTranslation(40, 0, 30)
You can subclass UITextField and override textRectForBounds and editingRectForBounds.
For example,
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 5, bounds.size.width - 20, bounds.size.height - 10);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x + 10, bounds.origin.y + 5, bounds.size.width - 20, bounds.size.height - 10);
}
Swift 4
yourTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10, 0, 10)
You can put the textfield background image in an image view,above the imageview put your textfield a space left.
-(void)textField:(UITextField *) textField didBeginEditing {
if ([textField.text isEqualToString:#""] || textField.text == NULL) {
textField.text = #" ";
}
}

UITextView - Horizontal Scrolling?

How can I create a horizontal scrolling UITextView?
When I set my text programmatically it adds automatic line breaks, so I can only scroll vertically...
titleView.text = #"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text.";
Thanks for your answers.
EDIT:
So far I tried this:
UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 , self.view.frame.size.width, 50)];
CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
yourScrollview.contentSize = CGSizeMake(textLength + 200, 500); //or some value you like, you may have to try this out a few times
titleView.frame = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height);
[yourScrollview addSubview: titleView];
NSLog(#"%f", textLength);
but I received: 'Threat 1: signal SIGABRT'
I have not yet done something like this, but I would try the following steps to accomplish this:
Create a UIScrollview *yourScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0 ,0 , self.view.frame.size.width, 50)]; //
Use CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
to get the final length of your text
Set yourScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times
Also set titleTextView.frame = CGRectMake(titleTextView.frame.origin.x, titleTextView.frame.origin.y, textLength, titleTextView.frame.size.height);
Make titleView a subview of yourScrollView: [yourScrollView addSubview: titleView];
Hope this gives you a good start!
EDIT: This Code will work:
Please notice I used a UILabel instead of a UITextView.
UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
titleView.text = #"this is a very long text. this is a very long text. this is a very long text. this is a very long text. this is a very long text.";
titleView.font = [UIFont systemFontOfSize:18];
titleView.backgroundColor = [UIColor clearColor];
titleView.numberOfLines = 1;
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
CGFloat textLength = [titleView.text sizeWithFont:titleView.font constrainedToSize:CGSizeMake(9999, 50) lineBreakMode:NSLineBreakByWordWrapping].width;
myScrollView.contentSize = CGSizeMake(textLength + 20, 50); //or some value you like, you may have to try this out a few times
titleView.frame = CGRectMake(titleView.frame.origin.x, titleView.frame.origin.y, textLength, titleView.frame.size.height);
[myScrollView addSubview: titleView];
[self.view addSubview:myScrollView];
[titleView release];
[myScrollView release];

How to change the XAxis used on my chart ShinobiCharts

I'd like to have the xAxis of my chart to look like this :
Jan, Feb, Mar, Apr, ..... Nov, Dec
Right now it's following it's default, numbering xAxis according to the number of Data Points.
How can i achieve this change to this axis ?
I've tried using Category Axis and setting an NSMutableArray containing these strings ("Jan", "Feb"...) as categories and with a numberRange going from 1 to 12 but it didn't work.
chart = [[ShinobiChart alloc] initWithFrame:chartEmbaixo.frame withPrimaryXAxisType:SChartAxisTypeCategory withPrimaryYAxisType:SChartAxisTypeNumber];
NSMutableArray * monthNames = [[NSMutableArray alloc] initWithObjects:#"Jan", #"Fev", #"Mar", #"Abr", #"Mai", #"Jun", #"Jul", #"Ago", #"Set", #"Out", #"Nov", #"Dez", nil];
SChartNumberRange * numberRange = [[SChartNumberRange alloc] initWithMinimum:[NSNumber numberWithInt:1]andMaximum:[NSNumber numberWithInt:12]];
SChartCategoryAxis *xAxis = [[SChartCategoryAxis alloc] initWithRange:numberRange];
xAxis.categories = monthNames;
chart.xAxis = xAxis;
first i use as my x axis
Edit how i make my x axis:
SChartNumberRange *r1 = [[SChartNumberRange alloc] initWithMinimum:[NSNumber numberWithInt:0] andMaximum:[NSNumber numberWithInt:2]];
SChartCategoryAxis *xAxis = [[SChartCategoryAxis alloc] initWithRange:r1];
xAxis.title = #"";
//xAxis.enableGesturePanning = YES;
xAxis.enableGesturePanning = YES;
xAxis.style.gridStripeStyle.showGridStripes = NO;
xAxis.style.majorGridLineStyle.showMajorGridLines = NO;
when you make you data points it should use the xValue as the x axis point.
like this:
dp.yValue = 1000;
dp.xValue = #"Jan";
the xValue should be set as the x point for that particular data point. This should work, but if it doesn't or you want to do something more complex you can extend this method from SChartDelegate protocol:
-(void)sChart:(ShinobiChart *)chart alterTickMark:(SChartTickMark *)tickMark beforeAddingToAxis:(SChartAxis *)axis
in this method you have the tickMark.tickLabelis the axis label for that given point where you can do your editing. Don't forget to verify what axis your on.
Hope this helps. If not tomorrow i can post you some code from my project (currently i don't have access to it from where i am)
Edit: currently i have this code:
- (void)sChart:(ShinobiChart *)chart alterTickMark:(SChartTickMark *)tickMark beforeAddingToAxis:(SChartAxis *)axis {
if (chart.yAxis == axis ) return;
for (UIView *i in tickMark.tickMarkView.subviews)
[i removeFromSuperview];
tickMark.tickMarkView.frame = CGRectMake(0, 0, 170, 75);
//center the marker at the right place because the size was changed
tickMark.tickMarkX = tickMark.tickMarkX - (tickMark.tickMarkView.frame.size.width/2) ;
tickMark.tickMarkY = 10;
//img
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"graph_bar_tag_2#2x.png"]];
img.frame = CGRectMake( 0, 0, tickMark.tickMarkView.frame.size.width, tickMark.tickMarkView.frame.size.height);
[tickMark.tickMarkView addSubview:img];
//label with the markView's size with 7px padding on the left and on the right
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake( 7, 5, tickMark.tickMarkView.frame.size.width-14, 15)];
label.backgroundColor = [UIColor clearColor];
//tikMark.tickLabel has an pair of indexes so that i can easily find the data for this particular data point and series.
label.text = [_dataSource getNameFor: tickMark.tickLabel.text];
label.textAlignment = UITextAlignmentCenter;
//color_other_light is a UIColor var
[label setTextColor: color_other_light];
[tickMark.tickMarkView addSubview:label];
...
}

How can I calculate NSString max length which UILabel can contain without truncate the text?

I have two UILabels, one above the other.
The top one has fixed size (2 Lines), and the bottom one can expand (0 lines).
The text which I using for the labels can be short and sometimes it can be very long.
How can I calculate the first UILabel max string length without cut a word in the middle?
//Here is the code which create the 2 labels.
titleView = [[UILabel alloc] initWithFrame:CGRectMake(70, 0, 200, 50)];
titleView.numberOfLines = 2;
titleView2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 250, 100)];
titleView2.numberOfLines = 0;
I've solved the problem with this method:
+ (int)getSplitIndexWithString:(NSString *)str frame:(CGRect)frame andFont:(UIFont *)font
{
int length = 1;
int lastSpace = 1;
NSString *cutText = [str substringToIndex:length];
CGSize textSize = [cutText sizeWithFont:font constrainedToSize:CGSizeMake(frame.size.width, frame.size.height + 500)];
while (textSize.height <= frame.size.height)
{
NSRange range = NSMakeRange (length, 1);
if ([[str substringWithRange:range] isEqualToString:#" "])
{
lastSpace = length;
}
length++;
cutText = [str substringToIndex:length];
textSize = [cutText sizeWithFont:font constrainedToSize:CGSizeMake(frame.size.width, frame.size.height + 500)];
}
return lastSpace;
}
CGSize textSize=[someString sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:sizeConstraint];
Then you can access the width and height:
float height=textSize.height;
float width=textSize.width;

Create UITextViews in a loop - Objective C

I'm working on an app in which I need to scan through a string to populate some UITextViews. Basically the data is like this:
Time Period: 10am-12pm
Temperature: 45F
Wind: 123 degrees # 5mph
Time Period: 1am-3pm
Temperature: 53F
Wind: 133 degrees # 2mph
Time Period: 4am-5pm
Temperature: 50F
Wind: 110 degrees # 7mph
The problem is that there is not a set number of time periods that are available at any given time. So I just have to loop through until I reach the end. Is there a way to create a textview inside a loop?
UITextView *textField1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
UITextView *textField2 = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
UITextView *textField3 = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
.....
So could I create the textfields at runtime until it reaches the end of the string or would I have to create the max number that it could possibly need then just use them if needed?
Thanks,
Andrew Boos
Sure you can.
First divide your data string using either [NSString componentsSeparatedByString] or [NSString componentsSeparatedByCharactersInSet] to get an NSArray of substrings.
//assuming your data is separated by newlines
NSArray * substrings = [data_input componentsSeparatedByString:#"\n"];
//loop over the substrings creating textfields
for (int i = 0; i < [substrings count]; i++)
{
CGRect frame = CGrectMake(0, i * 40, 100, 30);
UITextField * tf = [[UITextField alloc] initWithFrame: frame];
tf.text = [substrings objectAtIndex:i];
//add as subview
[view addSubview: tf];
//if you are not using ARC release the textfield
}
//disclaimer: written this on the go, may contain some spelling mistakes etc, but should be enough to get you going.