What are alternatives for these controls in iPhone - iphone

What are alternatives for these controls in iPhone
Radio Button
Check Box
Drop Down
x raise to Power y in UILabel
Hyperlink
Suggestion and answers will be appreciated, thanks.

Radio Button: UISegmentedControl
Check Box: UISwitch
Drop Down: UIPickerView
x raise to Power y in UILabel: no such thing, you need to draw it
yourself.
Hyperlink: Use a UILabel and attach a gesture recognizer for taps to
it (or a custom type button)

Almost all of these controls can be displayed using a UIWebView - if that's not an option, have a look at the UIWebView implementations and it should give you some kind of idea.
However, if you want native controls, these are probably the best options:
Radio Button: UISegmnetedControl
Check Box: UISwitch
Drop Down: UIPickerView (used in UIWebView).
x to the power of y in a UILabel is easy. Just replace your indices with unicode superscript characters... I use the following method to turn an integer into a string with superscript characters.
+(NSString *)convertIntToSuperscript:(int)i {
NSArray *array = [[NSArray alloc] initWithObjects:#"⁰", #"¹", #"²", #"³", #"⁴", #"⁵", #"⁶", #"⁷", #"⁸", #"⁹", nil];
if (i >= 0 && i <= 9) {
NSString *myString = [NSString stringWithFormat:#"%#", [array objectAtIndex:i]];
[array release];
return myString;
}
else {
NSString *base = [NSString stringWithFormat:#"%i", i];
NSMutableString *newString = [[NSMutableString alloc] init];
for (int b = 0; b<[base length]; b++) {
int temp = [[base substringWithRange:NSMakeRange(b, 1)] intValue];
[newString appendString:[array objectAtIndex:temp]];
}
[array release];
NSString *returnString = [NSString stringWithString:newString];
[newString release];
return returnString;
}
}
For a hyperlink, use a UITextView with Property Inspector -> Detection -> Links enabled and Editable behavior disabled. Of course this is also available in a UIWebView.

Related

How to add UILabel between two strings

i have this code to show some texts in a UITextview..i just want to add a UILabel between these strings.i am getting text in this formate, 1 this is first text 2 this is second text 3 this is third text,,i want to add or append UILabel between numeric character and text.my code for showing text is
NSMutableString *combined = [NSMutableString string];
for(NSUInteger idx = 0; idx < [delegatee.allSelectedVerseEnglish count]; idx++) {
[combined appendFormat:#" %d %#",
idx + 1,
[delegatee.allSelectedVerseEnglish objectAtIndex:idx]];
}
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
NSNumber * n = [f numberFromString:combined];
NSLog(#"N: %#", n);
maintextview.text =combined;
combined is the text in the above formate ,and maintextview is the UITextview.
am also getting the string range between two sentence
- (void)textViewDidEndEditing:(UITextView *)textView
{
if (textView == maintextview) {
mainpopupview.frame =CGRectMake(0, 0, 768, 1004) ;
[self.view addSubview:mainpopupview];
NSRange selectedRange = [textView selectedRange];
NSString *backString = [maintextview.text substringToIndex:selectedRange.location];
NSRange backRange = [backString rangeOfString:#" " options:NSBackwardsSearch];
NSRange backRangee = [backString rangeOfString:#" " options:NSBackwardsSearch];
int myRangeLenght = backRangee.location - backRange.location;
NSRange myStringRange = NSMakeRange (backRange.location, myRangeLenght);
NSString *forwardString = [maintextview.text substringFromIndex:backRange.location];
NSLog(#"%#",[[forwardString componentsSeparatedByString:#" "] objectAtIndex:1]);
NSLog (#"%#", [maintextview.text substringWithRange:myStringRange]);
NSString * myStringTxt = [[forwardString componentsSeparatedByString:#" "] objectAtIndex:1];
NSLog(#"1 %#", myStringTxt);
// maintextview.textColor = [UIColor yellowColor];
NSRange myStringRangee = [maintextview.text rangeOfString:myStringTxt];
[maintextview select:self];
maintextview.selectedRange = myStringRangee;
is there any way to do this.
Thanks in advance.
I don't know what user interaction you need here, but if you're just displaying the text for the user, but could you replace your UITextView with a UIWebView (loading static text via loadHTMLString:baseURL:, using html markup to set the color of the text)? I refer to the UITextView documentation:
This class does not support multiple styles for text. The font, color,
and text alignment attributes you specify always apply to the entire
contents of the text view. To display more complex styling in your
application, you need to use a UIWebView object and render your
content using HTML.
If you really need to do this with separate UI controls (much more complicated than just using a UIWebView) you don't "insert" the UILabel in the text, but rather you'd probably have one control for the text up to the point of the color change, another control for the text of a different color or what have you, another control for the rest of the text. It would get hairly pretty quickly (unless it was laid out like a grid, and even then it's a hassle compared to a UIWebView).

Incrementing NSArray on button click

I'm reading a text file line by line, and storing it into a NSArray.
What I want to achieve is to increment the index of my NSArray when the user clicks a button.
Code:
text.editable = NO;
NSString *fileString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"firstyea" ofType:#"txt"] encoding:NSUTF8StringEncoding error:nil]; // reads file into memory as an NSString
NSArray *lines = [fileString componentsSeparatedByString:#"\n"]; // each line, adjust character for line endings
NSString *wat = [lines objectAtIndex:1];
text.text = wat;
[self.view addSubview:text];
So I have to increment objectAtIndex by 1 whenever the user clicks a button.
I can do it in C++ by having a variable of type int, and saying something like objectAtIndex:counter++ but I don't know how to do the same thing in IOS.
You can't do it with NSArray, you need to use a NSMutableArray
You should take a variable of type static int.
For example:
static int counter=0; //define it globally
NSMutableArray *tempArray=[[NSMutableArray alloc] init];
[tempArray insertObject:#"Your Data From File" atIndex:counter++];
Note: Don't forget to release the array after you're finished using it!
Declare an integer i globally. #property int i;
in
viewDidLoad
, declare i=0;
-(IBAction)BtnClick:(id)sender
{
if ( i < lines.count )
{
NSString *wat = [lines objectAtIndex:i];
i=i+1;
}
}
Hope this helps.

reverse of a string

i want a program to display the reverse of a string. my program should have two textViews and a button. the string entered by user in a textView should be taken when we press the button and the reverse of that string should be displayed in the other textView. all the ib stuff to be done on window itself.
plz help me with code.
forgive me if you feel the question is simple or basic.
I would suggest you read in the values from the textview into an array then reverse the array, something like this where array1 is a list of values from your textview
NSMutableArray *tmp = [[NSMutableArray alloc] init];
for(int x=[array1 count] - 1;x>=0; x--){
[tmp addObject:[array1 objectAtIndex:x]];
}
What about this?
-(NSString *) revertString:(NSString*)stringToRevert
{
NSMutableString *reversedStr;
int len = [stringToRevert length];
reversedStr = [NSMutableString stringWithCapacity:len];
while (len > 0)
[reversedStr appendString:
[NSString stringWithFormat:#"%C", [stringToRevert characterAtIndex:--len]]];
return reversedStr;
}

markup text parser like stackoverflow's formatter in Objective-C

I'm in the process of creating a markup editor in Objective C. I require the following functionality:
Recognise the demarcation of a block eg **block**
Delete the start and end "tags" eg "The next text is **bold**" becomes "The next text is bold"
Determine the start and end positions of the marked-up text in the new context: "The next text is bold"
Edit:
As I may expand the syntax in the future (it will be very limited at the moment), it is important that parsing be top-down such that the start and end positions of the text always correspond with the resulting text. For this reason regex may not be the best solution.
What is the best way to do this?
In the end went for regex approach using RegexKitLite
The code below is not fully tested but does work with the case St3fan pointed out.
- (NSArray *) scanContent:(NSMutableString **)content {
NSMutableArray *tokens = [[NSMutableArray alloc] init];
NSArray *captureRegex = [[NSArray alloc] initWithObjects:
#"\\[\\[(.*?)\\]\\]",#"\\*\\*(.*?)\\*\\*", nil];
NSArray *tokenID = [[NSArray alloc] initWithObjects:
#"Italic",#"Bold", nil];
int index = 0;
for (NSString*capture in captureRegex) {
NSRange captureRange;
NSRange stringRange;
stringRange.location = 0;
stringRange.length = [*content length];
do {
captureRange = [*content rangeOfRegex:capture inRange:stringRange];
if ( captureRange.location != NSNotFound ) {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:[tokenID objectAtIndex:index] forKey:#"Token"];
[dictionary setObject:[NSNumber numberWithInt:captureRange.location]
forKey:#"Start"];
[dictionary setObject:[NSNumber numberWithInt:captureRange.length]
forKey:#"Length"];
[tokens addObject:dictionary];
for (NSMutableDictionary *dict in tokens) {
NSNumber *nRange = [dict objectForKey:#"Start"];
int start = [nRange intValue];
if (start > captureRange.location) {
nRange = [NSNumber numberWithInt:start - 4]; // Removing 4 characters
[dict setObject:nRange forKey:#"Start"];
}
if (start == captureRange.location) {
NSString *data = [*content stringByMatching:capture options:RKLMultiline inRange:captureRange capture:1 error:NULL];
NSLog(#"data: %#",data);
[*content replaceOccurrencesOfRegex:capture withString:data range:captureRange];
NSLog(#"Replaced Content: %#",*content);
}
}
stringRange.location = captureRange.location + captureRange.length -4;
stringRange.length = [*content length] - stringRange.location;
}
}
while ( captureRange.location != NSNotFound );
index++;
}
return tokens;
}
MarkDown Sharp, the markdown processor used on the StackExchange websites, is open source. Take a look at the file, perhaps you can see how they do it or port it to objective-c.
Perhaps better yet, take a look at this question: "What is the simplest implementation of Markdown for a Cocoa application?"
It links to an open source application called MarkdownLive which uses a C implementation of Markdown called discount, and also provides an objective-c wrapper for it.

Extract first word from UITextView

In my app I have a UILabel which is the title and a UITextView which is the description and I want the title UILabel to be the first word typed into the UITextView. Does anyone know how to do this?
If I understand well your problem, this code might do the trick:
- (void)textViewDidChange:(UITextView *)textView
{
NSString *text = _textView.text;
NSArray *elements = [text componentsSeparatedByString:#" "];
if ([elements count] > 0)
{
_label.text = [elements objectAtIndex:0];
}
}