How can I display fractional numbers in an UITextField, e.g. 5 and 1/5 should be displayed as the number "5" followed by the numerator "1" on top, a horizontal line, and the denominator "5" at the bottom. Should I subclass should I subclass a UITextField?
Is there any sample code somewhere that shows how can I achieve this?
If it is a commonly found fraction, you can find the unicode character (of 1/5 character) and then you can assign your textfield's text property something like this "\u{2155}" where 2155 is the unicode value of 1/5 character.
For finding out unicode values, you can use this website http://www.fileformat.info/info/unicode/
For more reading on displaying unicode character, you may use Apple Documentation
However, if the fraction is not a commonly found one, then for them there will be no Unicode characters. In such cases, you have to explicitly convert it into a string like "\(myNumeratorIntegerVariable)/\(myDenominatorIntegerVariable)"
Related
I would like to wrap text in a UITextView so that the textView does not extend beyond the edges of the screen. I'd like the first line to be indented x characters, then any subsequent lines indented another y characters. For example, if the string is:
aaaaabbbbbcccccdddddeeeeefffffggggghhhhh
I might want (for a particularly narrow screen that only displays 14 characters) with both x=4 and y=4:
aaaaabbbbbcccc
cdddddeeee
efffffgggg
ghhhhh
What I get now is:
aaaaabbbbbcccc
cdddddeeeeefffffgg
ggghhhhh
I could calculate how many characters fit on the screen and manipulate each string accordingly, but is there a simple way to do this using Swift functions?
I have a plist file which I decode to load data onto my application.
This plist file contains String type values that gets mapped to UILabel's text property.
I noticed that the truncating behavior of the text in the label is not always the same.
To be more specific, the three dots that are added when the text is truncated are, as opposed to my expectation, two kinds: one being ... and the other being ⋯ which appears to be this unicode character in this link.
I checked UILabel's attribute settings but I was unable to find any settings related to this behavior.
Has anyone else experienced this problem and standardized the truncating character to be ...?
Here is the image describing the problem mentioned above. Both labels have 2 lines and have new line escape character inserted between the first line and the second line of text. I am posting a link to this image because apparently I don't have enough reputation to post an image.
varying truncating characters of UILabel
IMO this is a bug in UILabel, and it may be worth opening a Feedback about it.
TL;DR: I recommend using TTTAttributedLabel.
Long-winded answer, because this was such an interesting question:
UILabel uses a different ellipsis based on the language script being truncated. As you've noticed, for most scripts, they use HORIZONTAL ELLIPSIS (…), or something very similar. But for Chinese, Japanese, and Korean (CJK), they use MIDLINE HORIZONTAL ELLIPSIS (⋯), or again, something very similar. The only other exception I've found is Burmese, which uses three circles that I don't recognize.
In my tests, all the following used …: Latin, Cyrillic, Bengali, Arabic, Hebrew, Hindi, Thai, Kannada, Nepali, and Mongolian (I kid. iOS can't layout Mongolian. Nobody can layout Mongolian, but it still uses …). UILabel even uses … for Lao, even though I thought ຯ was specifically for that, but I guess eventually everything becomes Latin.
The problem with UILabel being so clever for CJK and Burmese is that it decides what character to use exclusively by looking at the first character being removed. And it thinks SPACE is Latin (or at least not "special").
So what to do? My recommendation is probably to use TTTAttributedLabel, since it lets you configure the truncation character, and more importantly, is open source so you can fix it if it's not working the way you want.
The second option would be to truncate the text by hand using techniques like the one described in How to change truncate characters in UILabel?. There are probably better ways to do it using CTFrameGetVisibleStringRange instead of constantly shrinking the string until it fits, but I don't know if it's worth the effort. (If that path sounds useful, I could probably write up something that does it. It's just probably not worth the trouble.)
And the final option I know is to replace the SPACE character with an "equivalent" CJK character. The closest I've found that works is HANGUL FILLER (U+3164), but I don't like it. It's too wide, and I expect that it will make Korean uncomfortable to read (but I rarely try to read Korean, so I may be wrong here):
With SPACE: 안녕 하세요
With FILLER: 안녕ㅤ하세요
There's also HALFWIDTH HANGUL FILLER (U+FFA0), which is better, but UILabel seems to make it zero width (this may be a font issue, so maybe worth trying):
With SPACE: 안녕 하세요
With HALF: 안녕ᅠ하세요
let string = "안녕 하세요"
let filler = "\u{3164}"
label.text = string.replacingOccurrences(of: " ", with: filler)
OTOH, you may run into the same problem if you use any other non-CJK characters, like Latin punctuation or Arabic numerals. So this solution may not scale. And you should make sure that Voice Over properly ignores it.
I love the San Francisco font. And would like to use it if possible. I have a table view that shows a series of labels where each label is 6 hexadecimal digits, e.g.
F0:1A:2B
12:CE:88
The problem is that they don't line up nicely. I can enable the monospaced digits attribute for the font, but that doesn't account for the characters ABCDEF. I've tried the single monospace font (Menlo) and it looks terrible and out of place.
I'm toying with making 8 little labels one for each character (6 hex digits plus the two : separators), which seems like a huge kludge. Is there no other way. I wondered if there was a way to do something with AttributedString to get the hex digits to be same width?
If you want to use a proportionally spaced font, you'll have to draw the characters at the appropriate places. Otherwise, find a monospaced font you like.
For completeness sake, I should post what I ended up doing after accepting what #AaronBratcher confirmed.
First I created 5 separate UILabels for the following elements:
leftTwoDigits-leftSeparator-centerTwoDigits-rightSeparator-rightTwoDigits
I used constrains to constrain all of their baselines, and have 0 horizontal spacing betwixt each. The separator labels were set simply to : and normal color. The xxxTwoDigits labels were given the string AA. A is NOT the widest character, C is. But A is near it. C is too wide, and A is wide enough. The color of these labels was set to clear so it doesn't actually show up.
Then 6 more UILabels are added. Again the the same baselines. The first two are constrained to the leading and trailing sides of leftTwoDigits and constrained to match in width. Repeat for the other 4 cells. Make all centered. And populate them individually with the individual digits.
I have an iOS app which uses fixed width font label extensively.
After changing to the iOS 7 sdk and build target 6.1, all the label automagically replace occurences of three punctuation marks with an ellipsis character. This breaks a lot of stuff and looks weird, since the ellipsis character is not present in the font I use, and iOS sees fit to use one from a different font.
How do I stop this behaviour?
This is a ligature, and iOS seems to replace them automatically (like fl becomes fl). Seems like there are some options to disable them, see this question: Adjoining "f" and "l" characters
Alternative number three: insert a zero-width space (U+200B) between the dots.
(Posted as an answer per request of the OP)
One way around this is to replace the ASCII periods with a unicode 2024 character ("ONE DOT LEADER"). It looks exactly like a period but should not get converted automatically.
What you could do if this is widespread is to change all your UILabels to a subclass, MyLabel, and intercept messages to set the text, look for three dots, and if found change them to the unicode character above.
Yeah, this is a big PITA but I know of no other workaround.
EDIT
Another idea - find an open source UILabel (there must be at least one) and use it.
Another alternative : the ellipsis is a true character of its own. Why don't you try to add it yourself in your font (with Fontlab, FontForge or Glyphs) at the same width than the other characters?
Code on StackOverflow and other sites suggest this for displaying unicode on iOS:
[view1 setText:#"\uF300"];
This works for most unicode characters, however, this is failing for an Egyptian hieroglyphic unicode font (wait for large font to load).
The hieroglyphic unicode has one additional digit (i.e. "\uF3001" instead of "\uF300"). As a result, the wrong character is displayed and the "1" is left trailing.
It is clear that iOS can display the hieroglyphic font. For example, if you cut and paste this glyph () into a setText statement, after importing the TTF, the glyph can be seen on the iOS screen.
So the question is, how does one display a unicode character in a setText statement using a unicode font that needs five digits instead of four (i.e. "\uF3001")?
Actually, as long as you use a capital 'U' you can specify up to 8 digits like: "\U000F3001".
It is not possible through the method you are using:
https://developer.apple.com/library/ios/#documentation/MacOSX/Conceptual/BPInternational/Articles/NotesForLocalizers.html
Strings can include arbitrary Unicode characters with “\U” followed by
up to four hexadecimal digits denoting the Unicode character; for
instance, space, which is hexadecimal 20, is represented as “\U0020”.
This option is useful if strings must include Unicode characters which
cannot be typed for some reason.