Can any body tell me how to add bullet list and numbered list to the selected text in UITextView.
Check this question: iphone bullet point list
You might want to add the unicode char of bulletpoints to your lines (#"\u2022)
NSArray * items = ...;
NSMutableString * bulletList = [NSMutableString stringWithCapacity:items.count*30];
for (NSString * s in items)
{
[bulletList appendFormat:#"\u2022 %#\n", s];
}
textView.text = bulletList;
Related
I'm reading in a file, containing words and names, as a string. Then I'm breaking it into an array of strings. What I want to do is to print out the names that are also words. The words are spelled with only lower case letters and the names has a capital first letter. Thus, I want to order upper and lower cases the same so that Ii then can scan the array and receive the duplicates.
So what I have in my main.m file looks like this now:
int main(int argc, const char * argv[])
{
#autoreleasepool {
// insert code here...
NSString *wordString = [NSString stringWithContentsOfFile:#"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *words = [wordString componentsSeparatedByString:#"\n"];
Everywhere it says I should use a caseIntensiveCompare method, but I don't understand how it works, or how to use it in this particularly case.. When I search for it on google all I get is this:
NSString *aString = #"ABC";
NSString *bString = #"abc";
if ([aString caseInsesitiveCompare: bString]) == NSOrderedSame)
{
//The strings are ordered equal
}
It seems wrong, firstly because I only have the one string, and secondly I want it to actually order them the letters the same, not to check if they are ordered the same..
If someone could give me a hint of how to do this I would be VERY thankful!
Thanks in advance // Bjoern
Not sure whether i have understand your question properly. But how much i understood is you need to first store array string in mutable set then on the basis of that you can compare the existing one to the new one as represent below code. So that like that you can filter your array as well as identify duplicate both words and names. Below assuming words is you array which contains string values. So on the basis of that processing the further code.
NSMutableSet* existing = [NSMutableSet set];
NSMutableArray* newArray = [NSMutableArray
array];
for (id object in words) {
if (![existing containsObject:[[object
name]lowercaseString]) {
[existing addObject:[[object
name]lowercaseString];
[newArray addObject:object];
}
else
{
NSLog(#"duplicate name=%#", [object name]);
}
}
You could try something like this (explanation in the comments):
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
#autoreleasepool {
NSString *wordString = [NSString stringWithContentsOfFile:#"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error:NULL];
// Get all the words by separating on newlines & convert to lowercase
// Note: Assuming that the list doesn't contain duplicate strings
// (i.e. the same word or name twice)
// If it does, you should separate/add_to_set/get_all_objects/lowercase instead
NSArray *words = [[wordString componentsSeparatedByString:#"\n"]
valueForKey:#"lowercaseString"];
// Create a counted set to keep track of duplicate strings
NSCountedSet *bag = [[NSCountedSet alloc] initWithArray:words];
// Create a mutable set to add only duplicates
NSMutableSet *duplicates = [NSMutableSet setWithCapacity:0];
// Iterate and add words that appear more than once in the counted set
for (NSString *word in bag) {
if ([bag countForObject:word] > 1) {
[duplicates addObject:word];
}
}
NSLog(#"Words: %lu | Unique words: %lu | Duplicates: %lu", words.count, bag.count, duplicates.count);
// Output => Words: 235887 | Unique words: 234372 | Duplicates: 1515
}
}
Now duplicates is a Set of the strings that are both words & names (as per your requirement i.e. they only differ in the capitalization). You can get an array of the words by sending [duplicates allObjects].
I have a Combobox contains 30 items (1,2,...,30). I want to select item in Combobox, create dynamic NSTextField same item selected in Combobox. Then user input text to NSTextField, and then click on button to get all text of each NSTextField add to NSMutableArray.
I use bellow code to get text from NSTextField and add it to Array but it can only get from 1 NSTextField:
NSMutableArray * SSID_Arr = [[NSMutableArray alloc] initWithCapacity:x];
[SSID_Arr addObject:ssidtxt.stringValue]; // get text from NSTextField
NSLog (#"SSID_Arr : %#",SSID_Arr);
NSString *strSSID;
for(int j=0; j < [SSID_Arr count]; j++)
{
strSSID = [NSString stringWithFormat:#"\r\nSSID : %#", [SSID_Arr objectAtIndex:j]];
}
Do you have suggestion? Thanks in advance
just make
NSMutableArray *SSID_Arr = [NSMutableArray new];
NSMutableArray * SSID_Arr = [[NSMutableArray alloc] initWithCapacity:x];
gives an array with space reserved for x values.But there is no value in that position right now
So first fill up the array via a loop and then continue as
NSMutableArray * SSID_Arr = [[NSMutableArray alloc] initWithCapacity:x];
for (int i=0; i<x; i++) {
[SSID_Arr addObject:ssidtxt.stringValue];
}
Now the array contains x values and you can proceed.Please note the one textfield value is getting populated here x times.If you have to store all textfield values ,write a loop suitable to achieve all values and add it
Is it possible to add all the lines of an address to a text view, so far I can only get the last line to show up in the view adresslines.
self.adresslines.text = (NSString *)(CFDictionaryGetValue(dictionary, kABPersonAddressCityKey));
self.adresslines.text = (NSString *)(CFDictionaryGetValue(dictionary, kABPersonAddressStreetKey));
self.adresslines.text = (NSString *) (CFDictionaryGetValue(dictionary, kABPersonAddressZIPKey));
As you have it now you are setting the text view's text with each line of the address. You want to append, not replace. Something like this will work:
NSMutableString *text = [NSMutableString string];
[text appendString:(NSString *)(CFDictionaryGetValue(dictionary, kABPersonAddressCityKey))];
[text appendString:#"\n"];
[text appendString:(NSString *)(CFDictionaryGetValue(dictionary, kABPersonAddressStreetKey))];
[text appendString:#"\n"];
[text appendString:(NSString *) (CFDictionaryGetValue(dictionary, kABPersonAddressZIPKey))];
self.addresslines.text = text;
Of course you can add checks in here to avoid blank lines or add other formatting as desired.
I'm trying to save the content of a UITextView which contains lines of text formatted both RTL and LTR.
The problem is that UITextView checks only the first character to format direction. Let's assume I'm in "edit" mode and write this text (__ means spaces):
text1_______________________________________
____________________________________________אקסא
text2_______________________________________
and after saving we lost RTL for אקסא. Now I'd like to edit this text once again which now looks like:
text1_______________________________________
אקסא
text2_______________________________________
I'm not able to mix \u200F with \u200E directional characters in one UITextView.
How to manage this and save correctly bidirectional text from UITextView?
Here is a quick proof of concept using NSAttributedString :
- Split the text in paragraphs
- For each paragraph, detect the main language
- Create an attributed text with the correct alignmenent for the corresponding range
// In a subclass of `UITextView`
+ (UITextAlignment)alignmentForString:(NSString *)astring {
NSArray *rightToLeftLanguages = #[#"ar",#"fa",#"he",#"ur",#"ps",#"sd",#"arc",#"bcc",#"bqi",#"ckb",#"dv",#"glk",#"ku",#"pnb",#"mzn"];
NSString *lang = CFBridgingRelease(CFStringTokenizerCopyBestStringLanguage((CFStringRef)astring,CFRangeMake(0,[astring length])));
if (astring.length) {
if ([rightToLeftLanguages containsObject:lang]) {
return NSTextAlignmentRight;
}
}
return NSTextAlignmentLeft;
}
- (void)setText:(NSString *)str { // Override
[super setText:str];
// Split in paragraph
NSArray *paragraphs = [self.text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
// Attributed string for the whole string
NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc]initWithString:self.text];
NSUInteger loc = 0;
for(NSString *paragraph in paragraphs) {
// Find the correct alignment for this paragraph
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
[paragraphStyle setAlignment:[WGTextView alignmentForString:paragraph]];
// Find its corresponding range in the string
NSRange range = NSMakeRange(loc, [paragraph length]);
// Add it to the attributed string
[attribString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
loc += [paragraph length];
}
[super setAttributedText:attribString];
}
Also, I recommend reading the Unicode BiDi Algorithm to manage more complex use cases.
I have a TextView and I need to insert a word in a string from the location. Use this method to derive the location
NSInteger location = textView.selectedRange.location;
But I do not know how to put this into a string.
Thanks a lot
Is it what you looking for?
NSInteger location = textView.selectedRange.location;
UITextView yourTxtView;
yourTxtView.text = [NSString stringWithFormat:#"%d",location];
If you want to replace the selection with a word, the simplest would be something like this:
NSRange range = textView.selectedRange;
if (range.location != NSNotFound) {
NSString *replacement = #"some word";
textView.text = [textView.text stringByReplacingCharactersInRange:range
withString:replacement];
}
For an empty selection, this would insert the text at the caret position, if text is selected, it would be replaced. On iOS 5, using the methods in the UITextInput protocol would probably be more efficient, but also more cumbersome to use.