Drawing ruled lines in UITextView - uitextview

In a UITextView I want to draw ruled lines along with the text. For that, I subclass UITextView and overwrite drawRect. Seing a few post on the subject (including on this site), that seems the right way to start.
Here is the loop where I draw the lines:
for (int x=1;x<numberOfLines;x++) {
yPos=self.font.lineHeight*x+baselineOffset;
CGContextMoveToPoint(context,self.bounds.origin.x,yPos);
CGContextAddLineToPoint(context,self.bounds.size.width,yPos);
}
baselineOffset in this code is constant, meaning I step by self.font.lineHeight
but seeing the picture below (with the slow shifting between the lines and the text) I am obviously not using the right value for incrementing
the y coordinate (here called yPos). What should I use?
Has anyone got an idea of what could be the problem?
Thank you for any tip.

Use font.leading instead of font.lineHeight.

Related

Add double lines in the middle of another svg line

so I have a drawing program and I need to implement a "broken line", it is an SVG line with two another lines in the middle of this line, these two line needs to cross perpendicularly to the principal line, maybe this picture can help me to explain the problem:
This line can be drawn in any angle that the user choose
I don't really understand svg's so I'm having a lot of trouble implementing this.
Thank you
So I discovered one way to implement that using polylines and calculating the middle of the source and target coordinates, so when it changes I change the middle point too. After that, I created a marker-mid with the double lines.

React-Native Change Text With setNativeProps

Has anyone figured out a way to dynamically mutate text on the screen without triggering a render?
A large part of my screen utilizes setNativeProps for moving parts, meaning that the animations become lagged despite using shouldComponentUpdate. I would like to use the Text tag instead of the TextInput tag workaround suggested in this post for stylistic reasons.
Best case scenario is a workaround that involves setNaiveProps as it would follow the pattern of the rest of the screen; however, I currently plan to render all the numbers 0-9 on the screen an move them into place at the moment, so any help would be greatly appreciated!
As it turns out, you can actually format TextInputs the same exact way as Text elements (from what I have tested). For placing text horizontally, you have to set the width (something I had trouble with before). For those still interested in the original question however, you can nest TextInputs inside of a Text Element (one per text element because there is no justification and it automatically places them in a row). Styling applied to the Text Element will apply to the TextInput.

UIButton text not getting wrapped to several lines

I have the problem that the text on three UIButtons gets wrapped to several lines in the Interface Builder, but when I run the code, the text is larger than the button and in one line only. I tried setting NSLineBreakMode and NSTextAlignment, but both didn't help.
In the Interface Builder it looks correctly like this: http://imgur.com/WplkqQV while on the simulator it looks like this: http://imgur.com/0YPpEfU. Any ideas?
Thanks in advance.
There is certainly something very odd about your example; I can't reproduce it. You must be doing something to the buttons that you have not described in the information provided by your question. If you set up your button line break mode to be Word Wrap (in the nib), and if your constraints are sensible so that the button can get wider in landscape and narrower in portrait, then it will wrap in portrait and not in landscape, which I believe is what you want. Here are screen shots of a button in the Simulator on my machine (ignore the actual widths of the buttons; it's only an example; what's important is the text wrapping):
The real problem, however, is the height. You'll notice that that isn't changing. This is because a round rect button has an intrinsic height value. If you want the height to change, to make more vertical room for the wrapped text, you will probably need to subclass or intervene in the layout process after rotation. For example, I get pretty nice results like this:
-(void)viewDidLayoutSubviews {
CGRect f = self.button.bounds;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
f.size.height = 44;
else
f.size.height = 60;
self.button.bounds = f;
}

How can I draw to an XY position in Emacs?

I wanted to allow the Emacs cursor to move around freely outside of actual text (similar to virtualedit=all in Vim).
"Oh," I thought, "I'll just keep track of a virtual cursor and draw it to the screen myself."
But it turns out the actual native C drawing routines (such as draw_glyphs) seem to refer back to the buffer contents to decide what to draw (I could be wrong though).
My next idea was to make a giant overlay of all spaces so I'd have complete freedom where to put stuff. But an overlay only goes over ranges of actual text, so again, this does not seem to give me what I'm looking for.
Is this a reasonable goal without hacking the C code?
I believe the writeable area of a window is intrinsically limited to the buffer with which it is associated, i.e. you have to draw in an area where buffer content exists.
(One example of this limitation is the impossibility of drawing a vertical guide line in the 80th column to help the user identify long lines; currently the best possible implementation of such a feature is to highlight the "overflow" of each too-long line.)
You can do the same as what artist-mode does without adding spaces to the buffer:
when trying to place the cursor after the end of the line, just use an overlay with an after-string property which adds the spaces in the display without modifying the buffer.
Have a look at "artist-mode" (M-xartist-modeRET) - it allows you to draw in Emacs.
From the function documentation: "Artist lets you draw lines, squares, rectangles and poly-lines, ellipses and circles with your mouse and/or keyboard."
You can look at popup.el from the auto-complete package, which can pop up tooltips and menus and such at any position, including positions outside the contents of the buffer. Maybe that will show you how you can do it.

NSAttributedString: wrapping + truncation

I have a view that draws multicolored text inside UITableViewCell. To draw multicolored text I'm using NSAttributedString However, I would like to make it so that if the text is too long to fit into the view, the last visible line is truncated to display an ellipsis at its end.
Obviously this is very easy to do when drawing only a single line, as you can just set
kCTLineBreakByTruncatingTail for the line break mode of the paragraph style. The problem is that I want my text to wrap to fill the rectangle, and then only have the last line truncated with an ellipsis - setting the line break mode confines the whole text to one line.
Does anybody have any ideas of how I would go about this?
Many thanks in advance for any suggestions,
JC.
Create your CTFrame from your CTFrameSetter with the rectangle of your UITableViewCell. Then, you can get all the CTLines of your CTFrame and determine when they will cut off. To swap out the ellipsis, you could keep that drawn with a separate CTFrame and draw it over the overflowing text on the last line.
You can find working code here: https://stackoverflow.com/a/14612598/473067
It's a similar approach to what Heath suggested. But then all wrapped up in a shiny package.
Well, to activate text truncating in UILabel, you should re-set lineBreakMode parameter to NSLineBreakByTruncatingTail after setting attributedText.
textLabel.attributedText = attributedText;
textLabel.lineBreakMode = NSLineBreakByTruncatingTail;

Resources