How to add a small straight line (I mean like this: a̅ b̅ X̅) onto a character inside a string?

Viewed 338

I want to add small straight line onto some desired characters/numbers inside a string inside textview. I couldn't find a solution. Maybe using NSMutableAttributedString. Meanwhile, I mean doing this programmatically. There is strikethrough style, but not overstrike style. Or maybe adding the letters "a" and "_" with different .baseline values. But how to add both characters onto each other then? Is it possible?

EDIT: Due to make a try for the helpful answers below, I think to make the line at a spesific height is needed. "A\u{0305}" makes the up line very close to the character, as if it sticks. Is there a way to make it at specific height? For example, if we assume that all the keyboard-inputted characters are written inside every single boxes, the ceiling side of these boxes could be lined?

3 Answers

So this (note: see edit below) appears to be an "a tilde ogonek" (it's Lithuanian).

You can write it for instance as follows using these two Unicode characters:

let atildeogonek = "\u{0105}\u{0303}"
let title = "How to add a small straight line (I mean like this: \(atildeogonek)) onto a character inside a string?"

The first character is the a with an ogonek, the second one is the tilde.

EDIT: The initial question specifically asked about the character ą̃ ("a tilde ogonek") in the title, and I used this code to demonstrate how to use Unicode characters in a Swift string. After posting this answer, the question was edited to be more general about "a line above a character".

Programmatically, you could use a function like this:

func overline(character: Character) -> Character? {
    return "\(character)\u{0305}".first
}

That will take a character as input and return a new character (glyph) that has had the Unicode combining overline character added to it. It will return nil if adding the combining overline character fails.

The code print(overline(character:"A")!), for example, returns "A̅"

Or, if you want to add an overline to every character in a string, you could use a function like this:

func overline(characters: String) -> [Character?] {
    return  Array(characters).map { return "\($0)\u{0305}".first
    }
}

(I'm not sure if there are any characters for which the above will fail, so I'm not sure if force-unwrapping the result is safe. Thus I left the result of both functions to be optional Character/Array of Character.)

You can easily find the unicodes of ā or ą̃ by using the xcode's own Character Viewer. Just follow the following steps :

hit : Control + Command + SpaceBar

If you get a compact one like this, click the upper right corner icon to expand it.

enter image description here

When expanded, Click the settings gear in the corner . Select customize list.

enter image description here

select Enclosed Characters

enter image description here

Go down to the bottom and open Code tables then add Unicode.

enter image description here

Now, just search for your required Character and you can check its unicode value. here i am searching ā

enter image description here

to print unicode's value :

print("\u{0101}")
Related