UITextView issue in iOS 8 : text view not scroll when entering text - scroll

I am facing issue in iOS 8 in prior version is working perfect.
My text view is around 4 line when I am entering text more then 4 line text view automaticaly scroll up but in this case not scroll in iOS8.
Here is my code.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSString *updatedString = [textView.text stringByReplacingCharactersInRange:range withString:text];
textView.text = updatedString;
return NO;
}
Thanks in advance.

Try this code.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
NSString *updatedString = [textView.text stringByReplacingCharactersInRange:range withString:text];
//textView.text = updatedString;
[textView replaceRange:textView.selectedTextRange withText:text];
return NO;
}

In order to edit the text of a UITextView, you need to update it's textStorage field:
[_textView.textStorage beginEditing];
NSRange replace = NSMakeRange(10, 2); //enter your editing range
[_textView.textStorage replaceCharactersInRange:replace withString:#"ha ha$ "];
//if you want to edit the attributes
NSRange attributeRange = NSMakeRange(10, 5); //enter your editing attribute range
[_textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:attributeRange];
[_textView.textStorage endEditing];
Good luck

Related

NSTabView styled like iTunes for Yosemite

I'm trying to build an NSTabView styled in the same way as it is in this NSPanel/NSWindow in iTunes. this pops up when you edit a track.
When I add NSTabview, it appears with a tinted and bordered rect
I've tried everything I can think of to remove this rect, or set it to transparent. Has any one found a solution to this? Any pointers in the right direction would be extremely helpful.
You can create a tabless NSTabView and then you can add a NSSegmentedControl to select the active tab.
here's a programmatic solution for anyone interested:
- (void)createViews {
// tabview
self.tabView = [NSTabView new];
self.tabView.tabViewType = NSNoTabsNoBorder;
self.tabView.controlSize = NSRegularControlSize;
// tabview items
self.detailsTabItemView = [BPBaseView new];
NSTabViewItem *detailItem = [NSTabViewItem new];
detailItem.view = self.detailsTabItemView;
self.artworkTabItemView = [BPBaseView new];
self.artworkTabItemView.backgroundColor = [NSColor colorWithHex:BPCharcoalLight];
NSTabViewItem *artworkItem = [NSTabViewItem new];
artworkItem.view = self.artworkTabItemView;
[self.tabView addTabViewItem:detailItem];
[self.tabView addTabViewItem:artworkItem];
// segment control
self.segmentControl = [NSSegmentedControl new];
self.segmentControl.segmentCount = 2;
[self.segmentControl setLabel:#"Details" forSegment:0];
[self.segmentControl setLabel:#"Artwork" forSegment:1];
self.segmentControl.action = #selector(segmentControlClicked:);
self.segmentControl.selectedSegment = 0;
[self segmentControlClicked:self.segmentControl]; // force first view to show
// add views
[self.bottomContainerView addSubview:self.segmentControl];
[self.bottomContainerView addSubview:self.tabView];
}
- (void)segmentControlClicked:(id)segmentControl{
NSInteger index = [segmentControl selectedSegment];
[self.tabView selectTabViewItemAtIndex:index];
}

Unable to setLabel text Xcode tableview

I am trying to set a label text with the text i selected from my tableview. Here are my codes
- (void)viewDidLoad
{
// Location *l = [[Location alloc]init];
// [l view];
[super viewDidLoad];
// Do any additional setup after loading the view.
[pLabel setText:[self selectedText]];
NSLog(#"%#1", [self selectedText]);
}
-(id) initWithTextSelected:(NSString *) text {
self.selectedText = text;
[pLabel setText:selectedText];
NSLog(#"%#2", [self selectedText]);
return self;
}
I can't seem to set my label. In addition, NSLog 1 prints out null while NSLog 2 displayed my selected value.
i think the selectedText at the viewDidLoad is null because it is not retained and instead of using setText: use pLabel.text = selectedText;
At NSLog 2 ou are setting self.selected text.
but in viewDidLoad there is any code that setting the selectedText. Be sure that you are setting your "selectedText".

Deprecated setLineBreakMode Warning

I am getting a warning of deprecated method when using
[buttonLeft setLineBreakMode:UILineBreakModeWordWrap];
Is there any other substitute of this method?
try
[buttonLeft.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
For UILabel, the appropriate constant is now NSLineBreakByWordWrapping (instead of UILineBreakModeWordWrap):
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
can try the the following. it's bit long but i think it will work:
// we only want to add our custom label once; only 1st pass shall return nil
UILabel titleLabel = (UILabel)[self viewWithTag:TITLE_LABEL_TAG];
if (!titleLabel)
{
// no custom label found (1st pass), we will be creating & adding it as subview
titleLabel = [[UILabel alloc] initWithFrame:titleRect];
[titleLabel setTag:TITLE_LABEL_TAG];
// make it multi-line
[titleLabel setNumberOfLines:0];
[titleLabel setLineBreakMode:UILineBreakModeWordWrap];
// title appearance setup; be at will to modify
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setFont:[self font]];
[titleLabel setShadowOffset:CGSizeMake(0, 1)];
[titleLabel setTextAlignment:UITextAlignmentCenter];
[self addSubview:titleLabel];
[titleLabel release];
}
// finally, put our label in original title view's state
[titleLabel setText:title];
[titleLabel setTextColor:titleColor];
[titleLabel setShadowColor:titleShadowColor];
// and return empty rect so that the original title view is hidden
return CGRectZero;
}
Just check out and replace these enums.
In the previos of ios6 was:-
From IOS 6 it is:-

NSTextField handling Enter key presses strangely

I'm sure I'm just using it incorrectly, but what I'm doing is I have a NSTextField with a attributed string with a few characters of text in a different font at the end of it. When the user clicks the text field, the text at the end should disappear, and when the user finishes editing their text and removes focus from the text field, the text gets added back to the end of the string they entered.
It's working fine when then user tabs out of the box, or clicks somewhere on the window to remove focus from the textfield. The only time it doesn't work is if they hit the "return" key in the text box. The text still gets added to the end of their string in this case, but it's in the same font as the rest of the string.
Here is the relevant portion of my code. I've verified that both methods are being called in the same sequence both when I tab out of a field and when I hit enter in the field.
- (void) selectText:(id)sender
{
[titleText setStringValue: [NSString stringWithFormat:#"%#", userEditableText]];
}
- (void) textDidEndEditing:(NSNotification *)notification
{
userEditableText = [textField stringValue];
NSString* fullText = [NSString stringWithFormat:#"%# (%#)", userEditableText, nonUserEditableText];
NSRange range1;
range1.location = 0;
range1.length = [userEditableText length] - ([nonUserEditableText length] + 2);
NSRange range2;
range2.location = range1.length;
range2.length = ([[nonUserEditableText length] length] + 2);
NSRange range3;
range3.location = 0;
range3.length = [fullText length];
NSFont *font = [NSFont fontWithName:#"Arial" size:14.0];
NSMutableDictionary* stringAttributes = [[NSMutableDictionary alloc] init];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:fullText attributes:stringAttributes];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingMiddle];
[attrString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range1];
[attrString addAttribute:NSFontAttributeName value:font range:range2];
[textField setAttributedStringValue:attrString];
}
Not sure exactly what I did, but I changed around a bunch of code in several classes and now everything is working properly. Wish I knew what I did...

Clickable url link in NSTextFieldCell inside NSTableView?

I have a NSAttributedString that I'm using in a NSTextFieldCell. It makes several clickable url links and puts a big NSAttributedString inside the NSTextFieldCell. Whenever I am viewing the NSTextFieldCell normally and it's highlighted, I cannot click on the links.
If I set the TableView so I can edit each column or row, when I click twice, go into Edit mode and view the NSTextFieldCell contents, my links show up and are clickable. When I click away from the row, I can no longer see clickable links.
I have to be in "edit" mode to see the links or click on them.
I feel like there's some setting I'm just missing.
I don't think the tech note answers the question, which was how to put a link in an NSTableView cell. The best way I've found to do this is to use a button cell for the table cell. This assumes that only links will be in a particular column of the table.
In Interface Builder, drag an NSButton cell onto the table column where you want the links.
In your table view delegate, implement tableView:dataCellForTableColumn:row: as follows:
- (NSCell *) tableView: (NSTableView *) tableView
dataCellForTableColumn: (NSTableColumn *) column
row: (NSInteger) row
{
NSButtonCell *buttonCell = nil;
NSAttributedString *title = nil;
NSString *link = nil;
NSDictionary *attributes = nil;
// Cell for entire row -- we don't do headers
if (column == nil)
return(nil);
// Columns other than link do the normal thing
if (![self isLinkColumn:column]) // Implement this as appropriate for your table
return([column dataCellForRow:row]);
// If no link, no button, just a blank text field
if ((link = [self linkForRow:row]) != nil) // Implement this as appropriate for your table
return([[[NSTextFieldCell alloc] initTextCell:#""] autorelease]);
// It's a link. Create the title
attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont systemFontOfSize:[NSFont systemFontSize]], NSFontAttributeName,
[NSNumber numberWithInt:NSUnderlineStyleSingle], NSUnderlineStyleAttributeName,
[NSColor blueColor], NSForegroundColorAttributeName,
[NSURL URLWithString:link], NSLinkAttributeName, nil];
title = [[NSAttributedString alloc] initWithString:link attributes:attributes];
[attributes release];
// Create a button cell
buttonCell = [[[NSButtonCell alloc] init] autorelease];
[buttonCell setBezelStyle:NSRoundedBezelStyle];
[buttonCell setButtonType:NSMomentaryPushInButton];
[buttonCell setBordered:NO]; // Don't want a bordered button
[buttonCell setAttributedTitle:title];
[title release];
return(buttonCell);
}
Set the target/action for the table to your delegate and check for clicks on the link column:
- (void) clickTable: (NSTableView *) sender
{
NSTableColumn *column = [[sender tableColumns] objectAtIndex:[sender clickedColumn]];
NSInteger row = [sender clickedRow];
NSString *link = nil;
if ([self isLinkColumn:column] && (link = [self linkForRow:row]) != nil)
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:link]];
}
Now the link looks like a link, but a click on it is actually a button press, which you detect in the action method and dispatch using NSWorkspace.
Have you seen this technical note from Apple regarding hyperlinks?
Embedding Hyperlinks in NSTextField and NSTextView

Resources