I am writing an app that requires labels w/ musical accidental symbols, called sharp and flat (♯ and ♭, respectively). Specifically, I need a label that says c♯. It is irrelevant to the question what these two symbols mean within a musical context. My app's main font is Open Sans. However, because Open Sans doesn't specify a character for the sharp and flat symbols, it uses a generic sharp and flat symbol that I find unattractive. However, I know that Helvetica Neue (or the iOS system font) has some well made symbols for sharp and flat, so I decided to change the label's text to an attributed string so I could make the c Open Sans, and the ♯ Helvetica Neue (both size 24). I did this through the interface builder. Even though everything looks exactly the way I want in the storyboard, when I launch it in the iOS Simulator, the labels with attributed strings are significantly shrunken from the other labels. I should note that the label has 3 AutoLayout constraints, though these contrasts are positionally related only, not size related.
Things I have tried (unsuccessfuly):
Using only one font
Doubling the font size (was too big)
Removing autolayout constraints
I would greatly appreciate it if someone could explain how to correctly create these labels. I am happy to provide any more information that is needed.
Thank you
I discovered that the problem is that NSAttributedString does not (yet) support custom fonts through Interface builder. It looks like I will have to subclass. This question essentially boils down to be the same as this one: Attributed string with custom fonts in storyboard does not load correctly
Related
I am using the black and white circle in my project when I noticed that on ios chrome the white circle is larger than the black circle.
● - U+25CF
○ - U+25CB
In a desktop environment the circles appear to be the same size but have slightly different heights. The difference is not noticeable.
I am trying to make these circles the same size on ios chrome.
I feel like I have eliminated any variables and that the browser is responsible for the different sizes of these circles.
photo
Actually, as far as unicode is concerned, all characters are font dependant. When a certain character is not available, it is picked from a fallback font.
If you had not configured a custom font, or if these chars are not available in the font you picked then the different sized circles are in the default font used by chrome/ios.
So, you have two ways to go: either find a font that have the characters drawn in a way that suits you, and force that, or give-up using unicode characters for these glyphs and use inline images instead.
You could make use of SVG drawings which can be encoded within the HTML markup itself, that will ensure a consistent look.
I often use font data (.ttf or .otf) in Swift to change font of UILabel. But sometimes I use only show numbers in UILabel and think which is better using font data or image data of each numbers like "1_image.png", "2_image.png", "3_image.png"...
Of course if I want to show many characters like articles and messages I should import font file in project. But I'm not sure which is better to show only numbers in the specific situation like showing user game ranking and scores.
I think it is almost always better to use a font -- as long as you are allowed to distribute it. Some of the advantages of fonts are:
They scale
No need to bother about pixel resolutions
More options to align them (like baseline etc.)
Better support for accessibility (like adjusting the contrast etc.)
I used this code to measure size of an NSAttributedString.
static NSStringDrawingOptions const OPTS = NSStringDrawingUsesLineFragmentOrigin
| NSStringDrawingUsesFontLeading
;
CGRect b1 = [text boundingRectWithSize:targetSize options:(OPTS) context:nil];
This usually works well, but sometimes it shows incorrect result sometimes. Result size for same attributed-text object is about 10pt smaller than it actually rendered with UITextView, and I cannot choose correct size for the text-view.
Same code works well with simulators, but broke in actual device. Why this happens and what can I do to avoid this?
Test environment:
iPod 5th generation
iOS 7.1.2
Japanese language mode.
In my case, I built NSAttributedString with no attributes. Not even with NSFontAttributeName field. In this case, according to manual, system will choose predefined default font -- "Helvetica, 12pt".
But it seems the OS is choosing some different (which is wrong) sized font for the method in the environment. Maybe because of Japanese language mode. Anyway, UITextView rendering routine uses another font, and as a result, measured size becomes different with rendered size.
To avoid this, just set a font explicitly for EVERY characters in the attributed-string.
After specifying font for all characters explicitly, I confirmed that all the devices I have and simulators in Xcode 6 are working correctly. I believe there can be more odd cases if I don't set some metric-related attributes. Then I recommend to set all the metric attributes always.
I am not sure what will happen in other environment, but my case was solved with this. I hope this helps.
Update
I same issue occurs again even after applying this solution. This solution does not provide correct patch. Anyway this problem never appear on iOS 8.0, and I suspect this as a bug in the system framework.
How do I match the font pixel size given to me by my designer in PhotoShop to the correct font size in Xcode Interface builder.
For example, my designer is using Helvetica Neueu Regular 32px Font in his design.
I've used a few points to pixel translation sites, but it doesn't seem exact.
I have attempted to follow the answer from this question, but to no avail:
https://stackoverflow.com/a/6107836/1014164
You will never have perfect results when visually comparing a Photoshop comp to a real program. In fact, it's not un-common for a text layout to be different between different computers because version and operating system differences (as well as monitor layouts) cause the text to reflow every time it's edited.
Unless things are very much different in other versions of Photoshop, your designer hasn't specified 32px because Photoshop doesn't lay text out in pixels - it works in points/picas. The exact text rendering is also dependent on the document's resolution (which is different between print and screen).
The best you can do is get the text to look roughly proportional to the designer's intent. In modern iOS, most apps will use the user's customized font settings anyway.
I am wondering if it is possible to add small icons, that are available as png files, during launch time to a custom font I use in my app? The code should work on iOS5 and above. This would have several use cases. For example you could add labels with icons without using an image view and a label and layouting the stuff based on the text. I know that I have read somewhere, that apple uses this technique on their own apps, but I don't know if they add the icons on launch time or already in the design process into the font.
Any help would be appreciated.
I have simulated this by using a custom view that has a string and font property as well as one or more images. Create a mapping between special char values and images. In drawrect use the uikit category on NSString that lets you draw chars or strings. When u get to your special char you skip it and draw the image. Those draw methods return the size of the string/char so you can tell how much to move as you draw.
You can see this technique used on github, look for the CreditCard ObjectiveC. It's used to draw placeholders or chars in the PlaceHolder class
I think it is not possible to add png to font in device. Font file use vector graphics to represent every glyph. You can convert font file to SVG. But PNG is not vector graphics.
I use icon font in some of my projects. I have create a project to use icon font easily. https://github.com/JohnWong/IconFont. Icon font is always used in UILabel by setting text. But I think creating a UIImage from font is more flexible. UIImage can be used in Buttons, TabBar Items, Bar Button Items and so on. This is why I create this project. The README of this project is written in Chinese. But the demo project will show everything clearly.