How to add UILabel between two strings - iphone

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).

Related

Counting whitespace and blanks in UILabel text

I have a strike-out subview in my cell's labels and its width is set accordingly to the text in the row. The number of lines in the cell's label is set to 2, and when there are two lines I have two strike-outs set on the label.
This is what I'm doing for labels with just one line of text:
UIView *crossout = [[UIView alloc] init];
crossout.frame = CGRectMake(x, y, w + 5, 2);
crossout.backgroundColor = [UIColor colorWithRed: 0.0 / 255 green:175.0 / 255 blue: 30.0 / 255 alpha:1.0];
crossout.tag = 1;
[self.contentView addSubview:crossout];
I'm getting the width (w) by:
CGFloat w = [self.label.text sizeWithFont:[UIFont systemFontOfSize:15.0f]].width;
When I have a second strike-out, I simply subtract w from the length of my label to get the width of the second strike-out.
Problem though is that w doesn't account for white spaces or blanks in the label's text, so it won't always look consistent across the line breaks.
How do I calculate w so that it includes the white spaces and blanks?
as i know you can use this for UITextField but you can give it a try..
NSString *resultingString = self.label.text;
NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
if ([resultingString rangeOfCharacterFromSet:whitespaceSet].location == NSNotFound) {
//here you can put your code
}
To get the number of in-line whitespaces in your string you could try the following code:
NSCharacterSet* whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
NSArray* substringsArray = [yourStringToCheck componentsSeparatedByCharactersInSet: whitespaceSet]
NSLog(#"Number of whitespaces : %d", [substringsArray count]);
Or, maybe even better (allthough this probably doesn't account for tabs etc.):
NSLog(#"Number of whitespaces : %d", [[yourStringToCheck componentsSeparatedByString:#" "] count]);
Another option (NSMutableString):
NSUInteger replacementCount = [yourStringToCheck replaceOccurrencesOfString:#" " withString:#" " options:NSLiteralSearch range:NSMakeRange(0, [receiver yourStringToCheck])];

Crop last sentence from html string if it does not fit webView (iphone)

I have an array of texts that have bold parts in them. There is no rule where this bold word or sentence is, so i am using webView to display html string with bold tags where necessary. Now my webViews do not scroll anywhere and sometimes the text does not fit in them.
So here is my question:
I want to crop text so, that it would fit my webView and the cropping wouldn't be in the middle of sentence, but instead would crop out whole sentence if it doesn't fit. So in the end, the text should end with final sentence that fits.
What I did was strip html tags from the html sentence, calculate the height that the text takes up then remove last part of text, separated with "." (dot) if it exceeds required height.
Here is the code to do this.
NSString *returnedString = [[[NSString alloc] initWithString:htmlText] autorelease];
CGSize a = [[returnedString stripHtml] sizeWithFont:font constrainedToSize:CGSizeMake(sizeToFit.width, 999)];
NSMutableArray *sentences = [[NSMutableArray alloc] initWithArray:[returnedString componentsSeparatedByString:#"."]];
while (a.height > sizeToFit.height) {
_lastTextExceededLimits = NO;
if (sentences.count > 1) {
// -2 because last sentence is not deletable and contains html tags
NSString *sentence2 = [sentences objectAtIndex:(sentences.count - 2)];
NSString *stringToReplace = [NSString stringWithFormat:#"%#%#",sentence2, #"."];
returnedString = [returnedString stringByReplacingOccurrencesOfString:stringToReplace withString:#""];
[sentences removeLastObject];
} else
returnedString = [returnedString stringByReplacingOccurrencesOfString:[sentences lastObject] withString:#""];
a = [[returnedString stripHtml] sizeWithFont:font constrainedToSize:CGSizeMake(sizeToFit.width, CGFLOAT_MAX)];
}
[sentences release];
return returnedString;

What are alternatives for these controls in 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.

Custom keyboard and type letters

every one i create custom keyboard and i have problem
iam using my keyboard as textview.inputView = myKeyboardView;
my keyboard buttons have this code :
NSMutableString *text = [textPad.text mutableCopy];
NSRange selectedRange = textPad.selectedRange;
[text replaceCharactersInRange:selectedRange withString:#"A"];
textPad.text = text;
[text release];
so the problem is when i want edit a word from middle of my sentence if i select a word and add some letters to that my button write only ONE Character and if i write some other letters that begin end of sentence ! what can i do to solve this problem ?
//EDIT//:
PROBLEM SOLVED
My first guess is that you need to set selectedRange. Also consider using convenience constructors rather than explicitly creating mutable copies — you only mutate it once so it's not likely to be any more efficient.
NSString *text = textPad.text;
NSRange selectedRange = textPad.selectedRange;
NSString *replacement = #"A";
text = [text stringByReplacingCharactersInRange:selectedRange withString:replacement];
selectedRange = (NSRange){selectedRange.location + replacement.length, 0};
textPad.text = text;
textPad.selectedRange = selectedRange;

marquee effect in uitableviewcells textlabel.text

Is there a way to accomplish something like the html Marquee effect for a uitableviewcells textlabel.text?
You'll have to implement it yourself using a NSTimer. You would cycle trough the characters of your textLabel.text by taking one from the front and appending it to the back. In order to do this easily you could use a NSMutableString that you would manipulate using substringWithRange: deleteCharactersInRange: and appendString, and then set as textLabel.text after each character manipulation:
- (void)fireTimer
{
NSMutableString *mutableText = [NSMutableString stringWithString: textLabel.text];
//Takes the first character and saves it into a string
NSString *firstCharText = [mutableText substringWithRange: NSMakeRange(0, 1)];
//Removes the first character
[mutableText deleteCharactersInRange: NSMakeRange(0, 1)];
//Adds the first character string to the initial string
[mutableText appendString: firstCharText];
textLabel.text = mutableText;
}