Scrolling Content to Show Current Cursor Position on NSTextView - objective-c

I have an NSTextView control object wrapped with NSScrollView. What I want to do is to make the text string at the current position visible when it's hidden below the content view.
In reference to the picture above, with
NSUInteger cPosition = [[[textView1 selectedRanges] objectAtIndex:0] rangeValue].location;
[textView1 scrollRangeToVisible:NSMakeRange(0,cPosition)];
the scroll view will scroll itself to a position such that the selected string (document) will come at the bottom of the content view. (Line 11). That's not exactly what I want. I would like the scroll view to scroll itself to show the text string at the current cursor position when it's hidden below the content view (for example, at Line 14). How can I improve my code?
Muchos thankos.

One solution would be to use NSString's enumerateSubstringsInRange:options:usingBlock: method with the NSStringEnumerationByParagraphs option and passing the range that contains your cPosition to scrollRangeToVisible:. This would have the effect of making the paragraph that contains your cPosition visible.

Related

Textbox scroll to bottom in Avalonia

I have a textbox that I write info to that automatically creates a scroll bar when the screen is filled, and I'd like to scroll to the bottom of it. It seems like what I need to do is set the scrollbar Offset to some vector, but my problem is finding the ScrollViewer.
I can't use FindControl because it's not named anywhere in the xaml, and I can only change a few values using textbox.SetValue
The only way to scroll a TextBox currently is to set the CaretPosition. For example to scroll to the end of the text you could use:
textbox.CaretIndex = int.MaxValue;
For me it works only when I set textbox.CaretIndex to the length of the text. Assuming that property Logs (string type) is binded to textbox I used this
textbox.CaretIndex = Logs.Length

How can I get the caret's position in xaml?

I have a few textboxs and I want to achieve this funcion: when tap into each textbox, that specific textbox will move to the top and when user type in some data it can auto scroll and keep the cursor|caret always on top of the screen. I know that I can useScrollViewer.ChangeView(null,offset,null); to set the view, however, how can I get the cursor's position (y or vertical offset) though. This is WP 8.1 app.
According to the documentation for the WPF TextBox class here, there is a property called CaretIndex which "Gets or sets the insertion position index of the caret."
You might also be able to use the value of the SelectionStart and SelectionLength properties to find where the caret is, but if the SelectionLength is larger than zero, that might not work. Try the CaretIndex property.
I did not fully understand your requirement. Currently there is no property/method to get the current caret position in the text. But if you want to move your caret at the start of the textbox than use below code.
txtbox.SelectionStart = 0;
txtbox.SelectionLength = 0;
Edit
By using txtbox SelectionStart and textbox selection length you can get caret position.
int caretIndex = txtbox.SelectionStart + txtbox.SelectionLength;

How to insert UIView on left and indent the cell?

I'm trying to insert an UIView on the left of a UITableViewCell, but when I do that I don't know how to move/indent the whole cell to avoid overlaying the View.
If I use
cell.indentationLevel = 10;
Only the TEXT will be aligned, not the whole cell.
IE: http://i463.photobucket.com/albums/qq357/darkCB666/iOSSimulatorScreenshot19032014000530_zps44572bfa.png
I'm trying to do somehing like WhatsApp and Facebook Chat contact list (the cell lines are aligned with the text).
Try setting the separatorInset property instead, like this:
cell.seperatorInset = UIEdgeInsetsMake(0, 10, 0, 0);
That will inset the line and automatically adjust the default subviews along with it.
A common trick I've seen and used is to just lay out the cell with my own line to achieve the look I want. Rather than use the iOS default line, insert your own line at the position and length you need to account for the left-hand UIView.

Word wrap position in UILabel

When working with a muti-line UILabel object, is there a way to know at which position in the text the line wrapping will occur?
For example if the text of my label is 20 words, the display will be different according to the size of the box provided. I would like to know when (character position) the display is going to the next line.

How do you keep text from wrapping in an NSTableView using NSAttributedString

I have an NSTableView that has 2 columns, one for an icon and the other for two lines of text. In the second column, the text column, I have some larger text that is for the name of an item. Then I have a new line and some smaller text that describes the state of the item.
When the name becomes so large that it doesn't fit on one line it wraps (or when you shrink the window down so small that it causes the names to not fit on a single line).
row1===============
| image | some name |
| image | idle |
row2================
| image | some name really long name | <- this gets wrapped pushing 'idle' out of the view
| image | idle |
===================
My question is, how could I keep the text from wrapping and just have the NSTableView display a horizontal scroll-bar once the name is too large to fit?
Scrolling in Cocoa is implemented with the NSScrollView which is a view instead of a cell so if you really want to implement horizontal scrolling for table view cells I think you'd have to subclass the whole NSTableView and implement the feature there. My suggestion (without knowing the specifics of your situation, of course) is that you don't do that, though, since it's nonstandard behaviour and would probably entail quite a bit of work.
Truncate Instead of Wrap
If you're using a standard NSTextFieldCell, just select "Truncates" for its layout value in IB instead of "Wraps".
If you have a custom NSCell where you're doing your own drawing (I assume this is the case here), you should create an NSParagraphStyle, set its line break mode, add it as a value for the NSParagraphStyleAttributeName key in the NSAttributedString's text attributes dictionary.
An example:
NSMutableParagraphStyle *paragraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];
[paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
[attributedStr
addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0,[attributedStr length])];
Cell Expansion Frames
If you don't want to wrap your lines of text in the table view cells, the standard method of allowing the user to see the whole text is to use cell expansion frames which are enabled by default:
Cell expansion can occur when the mouse hovers over the specified cell and the cell contents are unable to be fully displayed within the cell.
If they're not working for some reason and you're using a custom NSCell subclass, make sure you implement -drawWithExpansionFrame:inView: and -expansionFrameWithFrame:inView: in your cell. Also make sure you're not returning NO in your NSTableViewDelegate for -tableView:shouldShowCellExpansionForTableColumn:row: (if you have one).
Adjust Width of Whole Table View?
If what you want to do is to adjust the width of a specific column (and thus the whole table view, possibly causing the enclosing scroll view's horizontal scroll bar to appear) such that the text its cells contain would never be truncated or wrapped, you can probably do that in your NSTableViewDelegate, for example, by calling -cellSize for each row's cell in that column and resizing the column to the largest value (you'll want to only do this when the values change, of course).