How do I use multiple different UIFonts in a UILabel? - objective-c

I have an UILabel in my UIView. The text of my label is dynamic: it's a web service call which returns to me the text that I will put in my label. Here's an example of the content of the label:
"this is an example of my text/nthis is the second line".
I would like to put the first line in a specific font (Helvetica-Bold 12px for example) and the second line in another font (Helvetica 15px for example). How i can do this?
Thanks for your answers.

Honestly I'd recommend using two separate UILabel instances, take your text and get an array via [string componentsSeparatedByString:#"\n"], then place object at index 0 in the first one, and if you have a second object, put it in the second label.

You could go for a static UIWebView and simply wrap your text into HTML+CSS. This will be much easier than doing all the layout in code.

Maybe what you are looking for is NSAttributedString.
Here is a example: iphone/ipad: How exactly use NSAttributedString?

Related

How to remove all text color attributes from a QTextDocument?

I've got a QTextDocument read from an HTML file; given a QString of HTML data named topicFileData, I do topicFileTextDocument.setHtml(topicFileData);. I then want to strip off all of the color information, making the whole document just use the default foreground and background brush. (I do not want to explicitly set the text to be black text on a white background; I want to remove the color information from the document.) (Background info: the reason I need to do this is that there are spans within the document that are erroneously set with a black foreground color, rather than just having no color information set, and that causes those spans to display as black-on-black when my app is running in "dark mode", when Qt changes the default text background brush to be black instead of white.)
Here's what I tried:
QTextCursor tc(&topicFileTextDocument);
tc.select(QTextCursor::Document);
QTextCharFormat noColorFormat;
noColorFormat.clearForeground();
noColorFormat.clearBackground();
tc.mergeCharFormat(noColorFormat);
This does not work, unfortunately; it looks like mergeCharFormat() does not understand that I want the clearForeground() and clearBackground() actions to be merged in to strip off those attributes.
I can do tc.setCharFormat(noColorFormat); instead, of course, and that does strip off the color attributes correctly; but it also obliterates all of the other character format info (font, etc.), which is not acceptable.
So, ideally I'd like to find an API that lets me explicitly remove a given text attribute from a QTextDocument. Alternatively, I guess I need to loop through all the spans of the QTextDocument one by one, get the char format of the current span, remove the color attributes from the format, and set the modified format back onto the span. That would be fine; but I have no idea how to loop over spans in that way. Thanks for any help.
Instead of creating a new instance of QTextCharFormat, update the current format and reapply it on the QTextEdit;
default = QTextCharFormat()
charFormat = self.textCursor().charFormat()
charFormat.setBackground(default.background())
charFormat.setForeground(default.foreground())
self.textCursor().mergeCharFormat(charFormat)
A sub-optimal solution that I have found as a workaround is to actually edit the HTML data string before I create the QTextDocument, using a regex:
topicFileData.replace(QRegularExpression("(;? ?color: ?#[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f])"), "");
This works for my situation, because all of the colors in my HTML file are set with color: #XXXXXX style attributes that can be stripped out of the HTML itself. This is fragile, however; colors specified in other ways would not be stripped, and if the body text of the HTML document happened to contain text that matched the regex, the regex would modify it and thus corrupt the content of the document. So I don't recommend this solution, and I won't be accepting it. If somebody can offer a better solution that would be preferable.

How to set IDEA better align parameters passed to some method?

I cannot make IDEA perfectly align parameters passed to some method when I break them into new line by Enter key.
Here is visually what I need. I have a method like this.
When I break parameters into new row each, it looks like this.
Even trying to align them with Tab key does not help.
You see how ugly this is, not to say it's not easy to reach such code. And all I want is too look like this:
(Note: this one I set via Space key).
I must have been doing something wrong as I can bet this can be set in IDEA. But where?
Bring up Settings panel :
Code Style > Java > Wrapping and Braces (tab)
Enable Align when multiline option in Method declaration parameters and Method call arguments
Apart from the way suggested by #Rangi Lin, also make sure that your
Code Style > Java > Keep When Reformating > Line Breaks
in your settings is also checked. Otherwise reformatting your code could realign your method params into a single line.

Ellipsize NSTextField?

Is there a way to ellipsize the text content of an NSTextField, instead of truncate?
So instead of:
The quick brown fox jumped over
It would say:
The quick brown fox jumped ...
I can't find anything in the documentation for this. What am I missing?
UPDATE:
Is there any way to ellipsize AND wordwrap--in other words, have multiple lines and ellipsize the last?
So this is a great question! Even though its exposed in IB, its not a property on the view or any subview. Instead its buried as a property on the cell used by NSTextField. So if you ask that object for its cell, you can then read or set the value: lineBreakMode.
Look in the class description for NSCell for all the options - truncateCenter is one (to get center ellipsis).
EDIT: the following thoughts were prompted by the updated question. Personally, I think trying to get that google code is way overkill and perhaps you can do something less complex by creating a mini-custom textField.
create a customer NSView object and give it a string and font property and some methods related to the actions below, and perhaps even a width property
essentially the idea is to use the Cocoa NSString category that lets you determine the length of string (and probably its height) from a string/font combo (I use this in UIKit, did use equivalents in Cocoa, but its been a while...)
one of your view's methods will be 'calculate'. When you get this, covert the string to an array of words using a single space as the separator (or make it more complex). Then, start computing the length of the drawn string, taking the first work, append a space and the second, etc, until you find you have exceeded he width. This is your first line string.
continue doing this for the number of lines that you want to draw (2? 3?). Calculate the length of the unicode char that does ... - its option semicolon as I recall - and keep that around.
in the last line, keep adding words until you exceed the length, and then back up a word at a time, verifying that the last appended strings string (minus a trailing space) but with the '...' char will fit in the space.
you can make this fancier by adding padding around the border etc.
once the calculation is finished (and you of course cache all the bits of the answer), your view is prepared when it gets 'drawRect:...'. You position at (0, bounds.size.height - 21) and draw the first segment, then move down 21 points and draw the second line, etc.
If I were to code this I would plan on 2-4 hours - its not trivial, perhaps the logic is a bit complex, but its straightforward. Good luck!
Although the correct answer was not accepted in this old question - scroll to the last answer - it includes the code Truncate the last line of multi-line NSTextField
The solution includes the use of ellipses and line wrapping combined.

Core Text's Paragraph's space is too huge on iOS

I used CoreText to render text as below:
Another very common typesetting operation is drawing a single line of text to use as a label for a user-interface element.
In Core Text this requires only two lines of code, one to create the line object with an attributed string and another to draw the line into a graphic context.
but it shows how to create an attributes dictionary and use it to create.
obvious there're 3 paragraphs. and I use default CTParagraphStyleSetting so that the ParagraphSpacing and ParagraphSpacingBefore is set to 0 by default.
But the rendered result shows the space is too HUGE
Any idea to reduce the paragraph space?
This might help:
Technical Q&A QA1698 - How do I work-around an issue where some lines in my Core Text output have extra line spacing?
u can try
kCTParagraphStyleSpecifierMinimumLineHeight
kCTParagraphStyleSpecifierMaximumLineHeight
kCTParagraphStyleSpecifierLineSpacing

How to actively change a label's string in visualworks smalltalk

I am trying to dynamically change the value of a label in an ApplicationModel dynamically during the running of my program.
I have tried:
(self builder labelAt: #Label4) labelString: 'Dynamic text here'.
But get a labelString not understood error.
When I run:
(self builder labelAt:#Label4)
I get "UndefinedObject". Which obviously suggests that I am not returning my Label object.
Try
(self builder componentAt: #Label4) widget labelString: 'whatever'
or
(self widgetAt: #Label4) labelString: 'whatever'
If you have a Model for your changing value it's pretty easy to use read-only InputField bound to your model. I dare to say having a Model for a changing value is generally a good idea.
If you define InputField as read-only and withou a border, it will look exactly like a regular text label. From a user's point of view the only difference is in ability to select and copy text from read-only input field, which is useful in many cases.
On the other hand, Labels can have a much richer look. You can use Text, ComposedText or Image for them.