I want to make text in label without breaking mid-word.
For example, I don't want work break like this.
In case of "Best of the test"
"Best of th
e test"
or
"Best of the te
st"
I just break this words as follows:
"Best of
the test"
or
"Best of the
test"
How can I do this?
My codes are as follows:
- (NSArray *)stringsFromText:(NSString *)string {
NSMutableArray *characterArray = [self arrayOfCharactersInString:string];
NSMutableArray *slicedString = [NSMutableArray array];
while (characterArray.count != 0) {
NSString *line = #"";
NSMutableIndexSet *charsToRemove = [NSMutableIndexSet indexSet];
for (int i = 0; i < [characterArray count]; i++) {
NSString *character = [characterArray objectAtIndex:i];
CGFloat stringWidth = [[line stringByAppendingFormat:#"%#", character] sizeWithFont:self.font].width;
if (stringWidth <= (self.frame.size.width - 10)) {
line = [line stringByAppendingFormat:#"%#", character];
[charsToRemove addIndex:i];
} else {
if (line.length == 0) {
line = [line stringByAppendingFormat:#"%#", character];
[charsToRemove addIndex:i];
}
break;
}
}
[slicedString addObject:[line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
[characterArray removeObjectsAtIndexes:charsToRemove];
}
if (self.lineBreakMode == UILineBreakModeWordWrap) {
slicedString = [self stringsWithWordsWrappedFromArray:slicedString];
}
return slicedString;
}
Please let me know. Thanks in advance.
label.lineBreakMode = NSLineBreakByWordWrapping;
Related
I have seen many of the answers but didn't get my answer. So that's why I decide to post a question. If anybody can find the helpful link or answer will be helpful.
Here is my array of dictionary:
<__NSArrayM 0x283ba04e0>(
{
failvalues = (
"Check 1"
);
fieldname = "Check 3";
fieldvalue = (
"Check 1",
"Check 2"
);
showtype = mandatory;
tagvalue = 0;
},
{
failvalues = (
Fail
);
fieldname = "Dropdown 3";
fieldvalue = (
Fail
);
showtype = mandatory;
tagvalue = 1;
},
{
failvalues = (
"Check 1",
"Check 4"
);
fieldname = "Check 4";
fieldvalue = (
"Check 1",
"Check 2"
);
showtype = mandatory;
tagvalue = 2;
})
So I want to check if the fieldvalue contains failvalues or not.
Below is the code which I have tried but it doesn't seem to work:
for (int i = 0; i< [arrFields count]; i++) {
if ([[[arrFields objectAtIndex:i]objectForKey:#"failvalues"] isKindOfClass:[NSArray class]]) {
if (![[[[arrFields objectAtIndex:i] objectForKey:#"failvalues"] objectAtIndex:0] isEqualToString:#""]) {
NSLog(#"Field Values %#",[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]);
NSArray *failValues = [[arrFields objectAtIndex:i] objectForKey:#"failvalues"];
for (int j = 0; j < [failValues count]; j++) {
if ([failValues containsObject:[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]]) {
NSLog(#"Contains %#",[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]);
} else {
NSLog(#"No fail values");
}
}
} else {
NSLog(#"No Fail Fields");
}
} else {
NSLog(#"Not an array");
}
}
EDIT:
This one I have tried but how to break both the loops
for (int i = 0; i< [arrFields count]; i++) {
NSArray *fieldValues = [[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"];
NSArray *failValues = [[arrFields objectAtIndex:i] objectForKey:#"failvalues"];
if (![[[[arrFields objectAtIndex:i] objectForKey:#"failvalues"] objectAtIndex:0] isEqualToString:#""]) {
//NSLog(#"Field Values %#",[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]);
for(NSString *value in fieldValues){
if ([failValues containsObject:value]) {
NSLog(#"Contains %#",value);
scanStatus = TRUE;
return;
}
}
} else {
NSLog(#"No Fail Fields");
}
}
Thanks in advance!
This is a classic use case for the feared and dreaded goto. It's well known that goto can create disastrously messy code, but in cases like this it'll make things cleaner. You have to know when to use goto, and definitely use it sparingly. But here's how I'd write your code:
BOOL found = NO;
for (NSDictionary *dict in arrFields)
{
NSArray *fieldValues = dict[#"fieldvalue"];
NSArray *failValues = dict[#"failvalues"];
if (![failValues[0] isEqualToString:#""]) {
for (NSString *value in fieldValue) {
if ([failValues containsObject:value]) {
NSLog(#"Contains %#",value);
found = YES;
goto leaveLoops;
}
}
}
}
leaveLoops:
if (found) NSLog(#"Found one.");
else NSLog(#"Didn't find one.");
And if you cannot bring yourself to use goto (you wouldn't be alone,) here's a gotoless alternative:
BOOL found = NO;
for (NSDictionary *dict in arrFields)
{
NSArray *fieldValues = dict[#"fieldvalue"];
NSArray *failValues = dict[#"failvalues"];
if (![failValues[0] isEqualToString:#""]) {
for (NSString *value in fieldValue) {
if ([failValues containsObject:value]) {
NSLog(#"Contains %#",value);
found = YES;
break;
}
}
if (found) break;
}
}
if (found) NSLog(#"Found one.");
else NSLog(#"Didn't find one.");
I have this array with strings and I want to randomize it and display the output in a label. But with this code I'm having always the same strange output which is "2"...Any ideas what I'm doing wrong?
- (void)viewDidLoad {
[super viewDidLoad];
self.quoteInput.delegate=self;
self.quoteLabel=_quotationLabel;
self.correctLabel.alpha=0;
self.wrongLabel.alpha=0;
_quoteInput.delegate=self;
_levelOne = #[
[words Quotes:#"First Quote."],
[words Quotes:#"Second Quote."],
[words Quotes:#"And so on."]];
}
-(void)randomQuotes{
for (NSInteger x = 0; x < [_levelOne count]; x++) {
NSInteger randInt = arc4random() % ([_levelOne count] - x) + x;
[_levelOne objectAtIndex:randInt];
NSString *one = [NSString stringWithFormat:#"%d", randInt];
self.quoteLabel.text = one;
}
}
- (IBAction)generateQuote:(UIButton *)sender {
[self randomQuotes];
}
EDIT
The words class:
+ (instancetype)Quotes:(NSString *)quotes
{
return [[words alloc] initWithQuotes:quotes];
}
Unless you can explain what [word Quotes:] method does, it should be much simpler than what you are trying to do.
_levelOne = #[#"First Quote.", "Second Quote.", #"And so on."];
-(void) randomQuotes{
NSInteger randInt = arc4random_uniform( [_levelOne count] );
self.quoteLabel.text = [_levelOne objectAtIndex:randInt];
}
I was looking into implementing hashtag autocomplete with objective-C as shown in the picture
I found it a bit difficult than expected. I'm looking more specific implementation for adding and deleting hashtags. For example, the hashtag should be deleted as a whole at once. I was wondering if anyone has similar experience implemented it and if there's a more efficient way implemented it. Thanks
I ended up writing some functions that I feel is a bit ugly but it works. Maybe there are some more efficient ways to implement it.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//user is a singleton instance
User *user = [User sharedUser];
user.autocompleteTableView.hidden = NO;
int identifiedTagsStringLength = [self.identifiedTagsString length];
int cursorLocation = range.location;
//insert characters
if (range.location >= identifiedTagsStringLength) {
NSString *newSearch =#"";
NSRange newRange;
if(identifiedTagsStringLength != 0) {
newSearch = [urlField.text substringFromIndex:identifiedTagsStringLength];
newRange = NSMakeRange(range.location - identifiedTagsStringLength, 0);
}
else {
newSearch = textField.text;
newRange = range;
}
NSString *substring = [NSString stringWithString:newSearch];
substring = [substring stringByReplacingCharactersInRange:newRange withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
if (cursorLocation > currentTagsRange) {
currentTagsRange = cursorLocation;
}
}
//delete tags
else {
if ([self.ranges count] != 0 && cursorLocation < currentTagsRange) {
int rangeLength = [self.ranges count];
int toBeRemovedIndex = 0;
for (int i = 0; i< rangeLength; i++) {
if (cursorLocation >= [[self.ranges objectAtIndex:i][0] intValue]
&& cursorLocation <= [[self.ranges objectAtIndex:i][1] intValue]) {
toBeRemovedIndex = i;
}
}
[self.tags removeObjectAtIndex:toBeRemovedIndex];
[self updateRanges];
NSString *outputString = #"";
for (NSString *tag in self.tags) {
outputString = [NSString stringWithFormat:#"%##%# ", outputString,
tag];
}
urlField.text = outputString;
self.identifiedTagsString = urlField.text;
currentTagsRange = [outputString length] - 1;
}
}
return YES;
}
- (void)updateRanges {
self.ranges = [[NSMutableArray alloc] init];
int startIndex = 0;
for (NSString *tag in self.tags) {
startIndex = [self.ranges count] == 0 ? 0 : [[self.ranges lastObject][1] intValue] + 1;
int tagLength = [tag length];
NSArray *range = [NSArray arrayWithObjects:[NSNumber numberWithInt:startIndex], [NSNumber numberWithInt:startIndex + tagLength + 1], nil];
[self.ranges addObject: range];
}
}
#pragma mark UITableViewDataSource methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (self.identifiedTagsString == NULL) {
self.identifiedTagsString = #"";
}
[self.tags addObject: selectedCell.textLabel.text];
[self updateRanges];
NSString *output = #"";
for (NSString *tag in self.tags) {
output = [NSString stringWithFormat:#"%##%# ", output, tag];
}
urlField.text = output;
User *user = [User sharedUser];
user.autocompleteTableView.hidden = YES;
self.identifiedTagsString = urlField.text;
currentTagsRange = [urlField.text length];
}
I want to change the following code
NSString* loop = #"";
for (int i = 1; i <= 5; i++)
{
loop = [loop stringByAppendingString:#"0"];
NSLog(#" %#", loop);
}
for (int i = 10; i >= 1; i--)
{
loop = [loop substringFromIndex:1];
NSLog(#" &#", loop);
}
to be result in the pattern of pyramid below. What should I do?
1
101
21012
3210123
432101234
and
0
01
012
0123
01234
012345
01234
0123
012
01
0
For second pattern you can use code:
NSString *loop=#"";
for (int i = 0; i <= 10; i++) {
if (i > 5) {
loop = [loop substringToIndex:[loop length] - 1];
}
else {
loop = [loop stringByAppendingString:[NSString stringWithFormat:#"%i", i]];
}
NSLog (#" %#" , loop);
}
For first
NSString *loop = #"";
NSString *spaces = #" ";
for (int i = 1; i <= 5; i++)
{
if (i==1) {
loop = [loop stringByAppendingString:[NSString stringWithFormat:#"%#%i", [spaces substringFromIndex:i], i - 1]];
} else {
loop = [loop substringFromIndex:6 - i];
loop = [[NSString stringWithFormat:#"%#%i",[spaces substringFromIndex:i], i - 1] stringByAppendingString:[loop stringByAppendingString:[NSString stringWithFormat:#"%i", i - 1]]];
}
NSLog(#" %#", loop);
}
I have data exported to excel it works fine.
But I have a little question
My output is exported like this:
What i would like to happen is this:
and this is my code to export:
-(void)exportCSV {
NSArray * data = [NSArray arrayWithObjects:entries,keys, nil];
NSLog(#"%#",data);
csv =[NSMutableString string];
for (NSArray * line in data) {
NSMutableArray * formattedLine = [NSMutableArray array];
for ( field in line) {
BOOL shouldQuote = NO;
NSRange r = [field rangeOfString:#","];
//fields that contain a , must be quoted
if (r.location != NSNotFound) {
shouldQuote = YES;
}
r = [field rangeOfString:#"\""];
//fields that contain a " must have them escaped to "" and be quoted
if (r.location != NSNotFound) {
field = [field stringByReplacingOccurrencesOfString:#"\"" withString:#"\"\""];
shouldQuote = YES;
}
if (shouldQuote == YES) {
[formattedLine addObject:[NSString stringWithFormat:#"\"%#\"\"%#\"", entries,keys]];
} else {
[formattedLine addObject:field];
}
}
NSString * combinedLine = [formattedLine componentsJoinedByString:#";"];
[csv appendFormat:#"%#\n", combinedLine];
NSLog(#"%#",csv);
}
}
Does the following do what you want?
Note that I have not considered quotation, I leave that up to you ;)
Also note that I assume that entries.count == keys.count
- (void)exportCSV {
NSArray *keys = #[#"T", #"R", #"RTT"];
NSArray *entries = #[#"-329180696", #"1243918297", #"-998693494"];
NSMutableString *csv = [[NSMutableString alloc] initWithCapacity:0];
for (int i = 0; i < entries.count; i++) {
[csv appendFormat:#"%#;%#\n", keys[i], entries[i]];
}
}
Output:
T;-329180696
R;1243918297
RTT;-998693494