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];
}
}
Related
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).
i'm going crazy on this problem:
I'm using AQGridView for show some image from an array that i retrieve from SQLite but i'm not able to filter the Grid with a UISearchBar that i put in the TitleView of a Detail zone in a SplitViewController. Can u help me with some logic passage or with an example?
Thanks!
SOLVED!
Recalculated the array _icons after removed all objects..
[_icons removeAllObjects];
searching = YES;
NSInteger numeroElem = [subcatList getSize];
for ( NSUInteger i = 0; i < numeroElem; i++ )
{
NSDictionary *itemAtIndex = (NSDictionary *)[subcatList objectAtIndex:i];
NSString *titolo_cat = [itemAtIndex objectForKey:#"titolo"];
NSComparisonResult result = [titolo_cat compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
ETC ETC.......
[_icons addObject: image];
}
};
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.
I have a text view in which I want to display NSArray *result as text of the text view.
For eg:
result={#"home",#"Office",#"Park",#"Market",nil};
textView text should be:
home
office
park
market
for(int i =0 ; i <[Array Count] ;i++)
{
self.textView.text = [NSString stringWithFormat:#"%# %#",self.textView.text, Array objectAtIndex:i];
}
Correct any spelling mistake.
You can use componentsJoinedByString: method, like this:
NSString *text = [result componentsJoinedByString:#" "];
You can use a different separator instead of #" ".
You can use a for loop as such:
NSString *str =#"";
for (NSString *tmp in result) {
str = [NSString stringWithFormat:#"%# %#",str,tmp];
}
If you want each entry on a new line replace the space with "\n".
How to truncate a string in Objective-C and then add the ellipsis at the end?
NSString *origString = #"A very long string blah blah blah";
const int clipLength = 18;
if([origString length]>clipLength)
{
origString = [NSString stringWithFormat:#"%#...",[origString substringToIndex:clipLength]];
}
Use one of these NSString methods to truncate, probably the last:
– substringFromIndex:
– substringWithRange:
– substringToIndex:
and then use the NSString method
– stringByAppendingString:
to add #"..." or whatever ellopsis you like.
For example:
NSString *newString = [[string substringToIndex:12] stringByAppendingString:#"..."];
For your reading pleasure, I recommend the NSString Class Reference.
In case you wish to truncate and add ellipsis to a string with the maximum being a specific width, here is an implementation that takes into account font and size:
+ (NSString *)stringByTruncatingString: (NSString *)string toWidth: (CGFloat)width withFont: (UIFont *)font
{
#define ellipsis #"..."
NSMutableString *truncatedString = [string mutableCopy];
if ([string sizeWithAttributes: #{NSFontAttributeName: font}].width > width) {
width -= [ellipsis sizeWithAttributes: #{NSFontAttributeName: font}].width;
NSRange range = {truncatedString.length - 1, 1};
while ([truncatedString sizeWithAttributes: #{NSFontAttributeName: font}].width > width) {
[truncatedString deleteCharactersInRange:range];
range.location--;
}
[truncatedString replaceCharactersInRange:range withString:ellipsis];
}
return truncatedString;
}
Don't need chuck of code for do this..
the easiest way to do this,
for drawRect
- (void)drawRect:(NSRect)dirtyRect{
NSString *theText = #"bla blah bla bhla bla bla";
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByTruncatingTail];
[theText drawInRect:dirtyRect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:style, NSParagraphStyleAttributeName,nil]];
}
hear I use dirtyRect for String's Drawing area you can change it as you wish.
for NSTextField
NSTextField *_warningTF = [[NSTextField alloc]init];
[_warningTF setStringValue:#"sfdsf sdfdsfdsfdsfdsfdsfdsf 1234566789123456789sfdsf dsf dsfdsf"];
[_warningTF.cell setLineBreakMode:NSLineBreakByTruncatingTail];
I wrote simple category to truncate NSString by words:
#interface NSString (TFDString)
- (NSString *)truncateByWordWithLimit:(NSInteger)limit;
#end
#implementation NSString (TFDString)
- (NSString *)truncateByWordWithLimit:(NSInteger)limit {
NSRange r = NSMakeRange(0, self.length);
while (r.length > limit) {
NSRange r0 = [self rangeOfString:#" " options:NSBackwardsSearch range:r];
if (!r0.length) break;
r = NSMakeRange(0, r0.location);
}
if (r.length == self.length) return self;
return [[self substringWithRange:r] stringByAppendingString:#"..."];
}
#end
Usage:
NSString *xx = #"This string is too long, somebody just need to take and truncate it, but by word, please.";
xx = [xx truncateByWordWithLimit:50];
Result:
This string is too long, somebody just need to...
Hope it helps somebody.
the drawWithRect:options:attributes:context method helps. you can try this:
[_text drawWithRect:_textRect options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil];