NSTextView Text Alignment - objective-c

well, I was searching for the code to make the "Left/Center/Right" Alignment for the NSTextView. I observe that in Text Menu, this functionality exists, and it is linked to the first responder calling the "alignCenter" selector. However I could not make it to work.
So I start to read the source code of TextEdit. It seems that Apple implement it and shows in the toolbar. However I was not able to find where it is. (It's not in any .nib file, and there's no selector related to align or centre or left, etc..)
So can anyone tell me: how to implement the function exactly like that in the Text Menu.
OR
Where the heck did Apple put the source code of aligning the text in their TextEdit program?

NSTextView inherits from NSText, which implements the alignCenter:, alignLeft: etc. actions.

You can use use the alignment property on NSTextView and set the alignment you want (.justified, .center, .left, .natural, etc)
let textView = NSTextView()
textView.alignment = .justified
or
textView.alignment = .center

Related

NSPopUpButton in NSToolbar such annoying

Problem solved!:
Just check the "Unified Title And Toolbar" option of the NSWindow and the 1pixel-down problem goes away!
To change the toolbar height just select the Toolbar Item - Custom View and change size in the Size inspector.
==============================
If you know Xcode 5s layout than you should recognise this:
I want to build it for my own. So I dragged a Toolbar in the Window and added a NSPopUpButton. Then I changed the PopUp Button Cell Style to Radio and turned off the Arrows. So far so good.
The first thing I noticed is that the Toolbars has different heights. Does anybody know how to change this behaviour (without subclassing NSToolbar)?
The second and more annoying thing I noticed is that if I choose an Item from the PopUp Button the Image for the NSMenuItem move 1 pixel down.
EDIT: Xcode NSMenuItems don't move 1pixel down
Any suggestions about that thing?
NSToolbar, sadly, can’t really be subclassed. It’s a poorly-written class that tries to be very “magic,” so it’s not even a subclass of NSView—you can’t control how it draws at all, it creates a private view.
You can set its “sizeMode” but I assume you’ve already done that and found that the number of pixels high isn’t what you want.
The easiest thing to do is just leave space for your widgets at the top of your window (above the document content) and have autolayout position your buttons for you. (I haven’t been able to use a real NSToolbar in years because of its limitations.)
As for the popUp menu being mis-aligned with the button: where the menu draws is basically hard-coded, so if you use a button style that NSPopUpButton doesn't expect then the menu will be offset some.
If you’ve already tried just unchecking the “draws border” flag on a default-style NSPopUpButton (one fresh off the palette), There are two solutions for to try: One is to keep trying different buttonStyles that look correct to your eye until you find one that’s not offset. Two is to leave the buttonStyle do the default for NSPopUpButtons but subclass the buttonCell and have it not draw the border (but still leave room for it).

Add / Remove buttons in NSOutlineView

I have a Source-View (NSOutlineView) with two Buttons at the bottom. I added an NSBox so that the items don't "shine through" when they're behind the buttons.
This works fine when the window is active:
But as soon as I deactivate the window the NSBox still has that active color, rather than a dimmed version to match the NSOutlineView's background color:
How can I make sure that those two colors always match. Also using a specific color is a bit of a hack since the color NSOutline uses might change at some point.
Update: Apple's Mail.app as well as Things seem to have a solution for that problem. :-/
#Neha put me on the right way to find a solution.
I write it in Ruby because I work with Rubymotion but it's easy to translate :)
Assuming you have a box outlet for the NSBox, you can set it to transparent when the window loses the focus and do the opposite when it becomes the key window, using the appropriate delegate methods:
def windowDidBecomeKey(notification)
box.setTransparent(false)
end
def windowDidResignKey(notification)
box.setTransparent(true)
end
And the result looks fine with the focus:
And without it:
The solution is to keep a reference to the NSOutlineView's backgroundColor property as it is a special NSColor that dynamically changes depending on the key status of the parent window. Set the color of your custom view to that that reference. When the window loses/gains key status, call setNeedsDisplay: on your custom view to redraw it using the new color. Use KVO to observe NSWindowDidBecomeKeyNotification and NSWindowDidResignKeyNotification. Note that pointer to the color stays the same, but the actual color represented by the reference changes. The solution is explained here.
In the attributes inspector of NSBox,
set display to transparent

Question about these UIButtons

How can I get a UIButton like the ones at the bottom of this picutre (move and delete)? Specifically, I want an image with a text just like that. http://4.bp.blogspot.com/_QLwms0mVa4w/SQN0MqPIpXI/AAAAAAAAAA8/lEikKn9eP_0/s1600-h/Screenshot+2008-10-25+15:31:21+-0400-1.png
Thanks.
For Jason:
The image sticks right beside the label. Is there anyway from IB I can set it to align to the left, while the label aligns to the right? Here's what it looks like right now:
What you can do is in Interface Builder, the UIButton can be set to a type of "Custom"
After you have done this, you can simply use your own image for the button and different images for each state of the button.
Although with this approach you won't be able to have additional text modified on it like the mail app has with "Delete (1)". However, if you don't need that then this solution will work for you.
Alternatively, if you just want an image stuck onto your existing button then there is an Image property in Interface Builder where you can slap on an image to your button.
If you need more functionality then you would probably have to create your own UIButton by subclassing to handle it.

Tips on implementing a custom UITextView interface on the iPhone?

I am trying to implement a control to edit text that will display the text in multiple colors. None of the solutions I have attempted yet have been good enough.
UITextView cannot accomplish this. All of the text must be the same color.
Using CoreGraphics to draw the text does not allow the text to be selected.
Using a UIWebView, DIV and PRE tags cannot be set to contentEditable on Mobile Safari.
Currently playing with using an off-screen TEXTAREA and an on-screen DIV to show the rendered text. This works pretty well, except supporting all of these at the same time seems impossible: click-to-type, click-to-move-cursor, click-and-hold-select/copy/paste.
Anyone have any tips on this predicament?
I've been trying to find any preexisting library out there that will accomplish this in a good way, to no luck. I'm open to any ideas!
Well, just pulling an idea out of my... let's say hat.
Could you put a transparent UITextfield over a view that draws the text? If the background was clear and the text color was clear the user could not perceive it but it should still respond to all commands. As the user enters and edits text you could draw the results on the view underneath.
I think the selection would work without any modification at all. When the user selected the clear text, it should create the illusion of selecting the drawn text automatically.
Like this one? StyledText http://three20.info/gfx/overview/styledtext.png It's in Three20 .
Here is an idea. I have no idea if it would work.
If you are only using colors, and not styles, the a UIWebView with colored text might layout text in exactly the same way as a UITextView. You could put a UITextView with invisible ink (text and background fully transparent) over a UIWebView and mirror the contents with colors in the html. I assume you can do scrolling with javascript along with the colored layout.

Draw Lines on gtk.TextView

I'm trying to show the "selection" of a certain sub-string in a
gtk.TextView by drawing a border around the word. The only way to mark
text in a TextView that I've found so far is by placing TextTags with
modified properties. This does not seem to offer a way to draw a border,
though, DOES GTK SUPPORT THIS OR IS THIS A PROBLEM WITH ONLT PYGTK
I figured out how to draw on a text view !!!
To begin with lets assume the reference to your gtk.TextView is in a variable called viewer, Inside one of ur classes
Also the draw function has to be called with an event called expose-event else the drawings will be refreshed and will not stay on the screen
The next part is the gtk.TextView consists of 7 types of gtk.gdk.windows on which u can draw
gtk.TEXT_WINDOW_WIDGET
gtk.TEXT_WINDOW_TEXT
gtk.TEXT_WINDOW_LEFT - not displayed by default
gtk.TEXT_WINDOW_RIGHT - not displayed by default
gtk.TEXT_WINDOW_TOP - not displayed by default
gtk.TEXT_WINDOW_BOTTOM
gtk.TEXT_WINDOW_PRIVATE
For the drawing to appear on gtk.TextView We have to draw on gtk.TEXT_WINDOW_TEXT
An Example Code is as shown Below
if(viewer!=None):
viewer.connect("expose-event", expose_view)
self.drawable=viewer.get_window(gtk.TEXT_WINDOW_TEXT)
def expose_view(self,window,event):
if(self.drawable!=None):
self.drawable.draw_line(self.drawable.new_gc(),1,1,30,30)
# (1,1) and (30,30) are the coordinates and u can give the values accordingly
In a gtk.TextBuffer tags are used to set one or more pre-defined text attributes. Without subclassing, this is limited to the properties of a gtk.TextTag, and doesn't include anything akin to a border or outline property. There is no difference between PyGTK and plain GTK+ in this regard.
While somewhat hacky, the easiest way to do what you want to do is to connect to the expose-event of your gtk.TextView, get the coordinates of your string and draw on event.window, which is the gdk.Window of the event provided in the expose callback.
(Note that you don't have to get and store the gtk.TEXT_WINDOW_TEXT window, you just need to check what window the expose event is for in the callback, probably ignoring the expose if it's not for the text window.)
Instead, you could presumably subclass one or more of TextBuffer/TextView/TextTag to add a border tag, but whether it's reasonable to do so is another question.