How to use font to write musical sheet - ios

I am trying to make an iOS app that can make possible to write and edit musical sheet.
I was wondering how can I visualise it.
I did not found any complete framework in swift that can make it possible.
But I found this font bravura, but i did not found any example of use.
This is an OpenType Font.
Reading the documentation I should be able to decide through unicode characters, how to change the position of the next element.
But no it doesn't seem to work. At first I thought it was because of the ligatures as not all editors support it, I'm not a font expert and I could be wrong.
So I used the NSAttributedString class to modify these parameters. But the result is still not what is expected.
This is my code :
textView.font = UIFont(name: "Bravura-Text", size: 40)
let text = "\u{E014}\u{EB99}\u{E0A4} \u{E014}\u{EB91}\u{E0A4}"
let attributes = [
NSAttributedString.Key.font : UIFont(name: "Bravura-Text", size: 40)!,
NSAttributedString.Key.ligature : 2
] as [NSAttributedString.Key : Any]
let attributedString = NSAttributedString(string: text, attributes: attributes)
textView.attributedText = attributedString
E014 = Displaying a 5 line staff
EB99 = Lower by two staff positions
EB91 = Raise by two staff positions
E0A4 = The black notehead
This is the result :
result
Any suggestions to make it possible to use this font and its features on iOS?
I would like to write a musical sheet like this one.

Layout of music notation require higher-level layout rather than just displaying a line of text. (Analogous to Web pages using HTML as high-level layout rather than just lines of text.) Fonts can provide the basic symbols, and all of the basic symbols are encoded in Unicode. But you'll need some library that can read a music-notation markup language and layout the content.
You can probably find the kind of resources you'll need at https://www.w3.org/community/music-notation/

Related

Make selected string of text view Bold, Italic, Underline like native "Notes" app of iOS

Is there any help to make selected string of text view Bold, Italic, Underline like native "Notes" app of iOS. Please give me helpful links. I am tired of searching for the whole day. Many Thanks.
I have attached my code, to make attributed string Bold and Italic both like native app of iPhone "Notes".
attributedString.beginEditing()
attributedString.addAttributes([NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(app_delegate.settings.chatFontSize))], range: range)
attributedString.addAttributes([NSFontAttributeName: UIFont.italicSystemFont(ofSize: CGFloat(app_delegate.settings.chatFontSize))], range: range)
attributedString.endEditing()
But its giving only Italic string, not also Bold. I need both Italic and Bold. Thanks.
The problem in your code is that you are setting the italic font and overwriting the bold one you've just set. What you need is to use UIFontDescriptor with Symbolic Traits as you can see in this SO answer. So just initialize your system font, get its font descriptor and add traitBold and traitItalic to it. Then you just need to initialize your new Font using UIFont initializer init(descriptor: UIFontDescriptor, size pointSize: CGFloat):
Swift 4 code:
attributedString.beginEditing()
let systemFont: UIFont = .systemFont(ofSize: 32)
if let descriptor = systemFont.fontDescriptor.withSymbolicTraits([.traitBold, .traitItalic]) {
let systemFontBoldAndItalic = UIFont(descriptor: descriptor, size: 32)
attributedString.addAttributes([.font: systemFontBoldAndItalic, .underlineStyle: NSUnderlineStyle.styleSingle.rawValue], range: NSRange(location: 0, length: attributedString.length))
}
attributedString.endEditing()
Take a look at NSAttributedString. You can use that to create strings that have mixed attributes at different ranges.
In Objective-C, the mutable version, NSMutableAttributedString has methods like setAttributes(_:range:) that let you change the attributes of an attributed string in a specified range.
So you'd initialize an NSMutableAttributedString starting from a normal String object, and then use setAttributes(_:range:) function to set different attributes like Bold, Italic, etc on a range of the string.
(No, I don't have sample code handy.)
Although the question is old but it may helps someone.
Using NSMutableAttributedString won't solve the problem, because if you type any where before the customised text the range will be different. Therefore, the customisation will shift to satisfy the updated range.
I think using HTML is better solution if you would have a very long string.
You may consider using some open source libraries, like ZSSRichTextEditor
Hope that help.

How can I get a custom font weight for the system font, in-between regular and light?

I'm using the iOS system font (San Francisco for iOS 9+) in my custom keyboard extension.
I've noticed the font shown in the system keyboard has a lighter weight than UIFontWeightRegular but it is also bolder than UIFontWeightLight.
How can I implement a font in my own keyboard with the same weight that's used in the stock keyboard?
The systemFontOfSize:weight: API allows you to specify any weight as a CGFloat, and iOS will return the most appropriate font.
For example, instead of UIFont.systemFontOfSize(17, weight: UIFontWeightRegular) you could do UIFont.systemFontOfSize(17, weight: 0). Play with the weight value to determine if you can get a font that appears as desired.
Another option could be to try using HTML & CSS with an attributed string. Using font-weight you can specify a value between 100 and 900, where 400 is equivalent to the normal font weight.
var htmlString = "<p>A</p>"
htmlString += "<style>p{font-family:'-apple-system'; font-size:17px; font-weight:300}</style>"
let htmlData = htmlString.dataUsingEncoding(NSUnicodeStringEncoding)!
let attributedString = try! NSAttributedString(data: htmlData, options: [
NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding
], documentAttributes: nil)
self.aLabel.attributedText = attributedString
Update: Trying this out, neither solution allows finer control over the font weight. Both will render the text with the closest font for the given weight - Regular or Light but nothing in-between. You may need to resort to custom text drawing if you wish to add a thicker stroke to a thin font. You might investigate drawing via NSAttributedString's drawing API. You may be able to apply a custom stroke width via NSStrokeWidthAttributeName. I have not tried this myself but it was a thought that might be worth investigating.

Get UIFont for content size category

UIFont provides the +preferredFontForTextStyle: method to get a font instance with the proper size based on the user's selected content size category and the given UIFontTextStyle.
What I would like to do is get a font for a given text style and content size. Something like +fontForContentSizeCategory:andTextStyle:.
Unfortunately I cannot find anything similar to that in the headers for UIFont or UIFontDescriptor.
Any idea on how to achieve this?
Thanks
Unfortunately, both +[UIFont preferredFontForTextStyle:] and +[UIFontDescriptor preferredFontDescriptorWithTextStyle:] rely on -[UIApplication preferredContentSizeCategory] internally. They use a private function _CTFontDescriptorCreateWithTextStyle to retrieve a CoreText font descriptor with specific text style and size category, and that's eventually based on a category → size mapping from the configuration file CoreTextConfig.plist stored somewhere, but I assume you wouldn't want to use private APIs.
While hesitantly implying a possibility to dynamically swizzle -[UIApplication preferredContentSizeCategory] to trick +[UIFontDescriptor preferredFontDescriptorWithTextStyle:] into returning a font descriptor for the size class you want, I can't recommend any specific approach to this. You can retrieve a font descriptor like this:
let descriptor = UIFontDescriptor(fontAttributes: [ UIFontDescriptorTextStyleAttribute : UIFontTextStyleBody ])
but it won't contain a size attribute, so you would be left with trying to come up with a category → size mapping yourself.
Since iOS 10.0 this is possible using UITraitCollection:
let traitCollection = UITraitCollection(preferredContentSizeCategory: contentSizeCategory)
let font = UIFont.preferredFont(forTextStyle: textStyle, compatibleWith: traitCollection)

Fonts lineHeight equalization in UITextView

I have a UITextView set with some text with both chinese and latin characters.
As you can see, the lineSpace is different depending if there is a latin character or not in the line above. Obviously, I need to remove this difference and to equalize the lineSpace.
Here are the statements :
I work with an AttributedString
The font is different depending if the character is latin or chinese
The point above needs to stay true
The lineHeight is different between the 2 fonts
I almost sure than the reason of my problem comes from this last point but since the lineHeight of the font is not editable, I'm a bit stuck.
I already try to play with those properties
style.minimumLineHeight = mylineSpacing;
style.maximumLineHeight = mylineSpacing;
style.lineHeightMultiple = mylineSpacing;
style.lineSpacing = 0;
NSDictionary *attributes = #{
NSParagraphStyleAttributeName : style
};
It doesn't work. I'm thinking about using CoreText or even CoreGraphic to redraw all the characters one by one but maybe there is an easier solution. And if not, which Kit should I use ?
PS : I'm not chinese and I used random chinese text for this screenshot and I apologize to an eventual chinese stackoverflow user if my text looks stupid / no sense / or offending in any way.

Swift - iOS 8 - NSKernAttributeName character spacing problems

Part of my application facilitates the drawing of stored string values to a graphics context to be viewed by the user. This is being done with an attributed string with the below Attributes.
//adding the attributes to the NSAttributedString
let textStyle = NSMutableParagraphStyle.defaultParagraphStyle().mutableCopy() as NSMutableParagraphStyle
let textColor = UIColor.blackColor()
let boxTextFontAttributes = [
NSFontAttributeName : font!,
NSForegroundColorAttributeName: textColor,
NSParagraphStyleAttributeName: textStyle,
NSKernAttributeName: (7.62),
]
I'm seeing a problem with the NSKernAttributeName character spacing on the output.
I have 2 Strings, both with the same CGRect dimensions (but different y-positions (one below the other)), both upper case, same font & size.
Whenever it comes to a 'thinner' character in one of the strings, such as an "I", it looks to impact the spacing of the following characters.
I basically need to have each character in both strings to be vertically in-line to the string above/below regardless of what character it is? I can only assume that even though it may have a set spacing between the characters, each character still has an individual 'width' of sorts that impacts the rest. Is there a way to standardise this?
Any help would be appreciated - (apologies for not being able to post a picture due to lack of reputation points)
A monospaced font corrected the issue of the character alignment. Some example monospaced fonts available in iOS 8 are Courier & Menlo. Useful post: What is a monospace font in iOS?

Resources