NSAttributedString: letters interference with arabic font - ios

I used arabic font called: "Hafs", it is from here:
http://fonts.qurancomplex.gov.sa/download/UthmanicHafs1Ver09Font.zip
I used it to draw Ayat from Holy Quran, but some letters are interfered with others, as shown in picture:
how to represent right text without interference?
thanks
EDIT:
here is another photo from "Pages" application to show you the original text, i coloured words that corrupted in iOS simulator by red colour
EDIT2:
I set attributed strings follows:
1- i paste the string into TextView in the storyboard.
2- i wrote this code in the related viewcontroller:
let font:UIFont! = UIFont(name: "KFGQPC Uthmanic Script HAFS", size: 30)
self.txt2.attributedText = NSAttributedString(string: txt2.text, attributes: [NSFontAttributeName:font])

Related

UIButton word wrap doesn't work when entering text?

I've got a UIButton, it's a simple segue to another page.
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line.
However, it is all left justified. When I select "Align Centre" (using the buttons just under the "Title", the word wrap no longer works and simply runs .... so you can't see it all. (e.g. "next pa" instead of "next page")
Am I missing something here? It seems like such a trivial thing to do! There's an old answer here can't get word wrap to work on UIButton but it's both old and uses code - surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
I've set Title to attributed and then selected word wrap. This works fine, the second word wraps down to the next line. However, it is all left justified.
Once you've decided to use an attributed string, you must do everything with the attributed string. So give your attributed string a paragraph style that centers the text.
let para = NSMutableParagraphStyle()
para.alignment = .center
para.lineBreakMode = .byWordWrapping
let s = NSAttributedString(
string: "Hello World", attributes: [.paragraphStyle : para])
self.button.setAttributedTitle(s, for: .normal)
You will also need to set the button's title label to allow multiple lines.
self.button.titleLabel?.numberOfLines = 0
Result:
surely you don't need code to centre the button text if you want to word wrap it to 2 lines!?
Not to center it, no; you can set the centering in the storyboard. So you could eliminate the first batch of code and configure it in the storyboard. But you must use code to turn the title label into a multiline label:
self.button.titleLabel?.numberOfLines = 0
There is no way to do that in the storyboard, because you have no access to the title label there.
I've just been playing round with this and I've found it works if you set it to 'character wrap' rather than 'word wrap' once you've selected centre alignment.
If anyone has a better solution please add it, as I guess this might have issues if you slightly change the width etc when using auto layout for different screen sizes etc if you want it to adapt its width so this might not be the best solution but it does work

Different Text Attributes per Link in UITextView/UILabel Swift

I would like to style each link differently within either a UITextView or UILabel. I am currently using UITextViews with links but I want URLs to appear purple and usernames to appear black. I've also looked into TTTAttributedLabel but cannot seem to find out how to accomplish this.
textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
attributedString.setAttributes([NSLinkAttributeName: url, range: range)
attributedString.addAttributes([NSForegroundColorAttributeName: UIColor.purple], range: range)
In the example above, which is what I have tried, the link will be orange, not purple. If I do not set linkTextAttributes, links will be blue. How do I override linkTextAttributes with different attributes per link?
linkTextAttributes can't designate the text behavior for each word.
Have you try this?
https://github.com/rinov/RegeributedTextView
and
textView.addAttribute("text1", attribute: .linkColor(.orange))
textView.addAttribute("text2", attribute: .linkColor(.purple))
NSRange is automatically calculated.

Set label font with bold and italic

I'm trying to set a specific part of my label to bold and italic with NSMutableAttributedString, but I can only get one or the other. The font that I'm using is Clear Sans. Setting the entire label's font on the storyboard to Bold Italic works fine, but I only need part of it. So I used
for family in UIFont.familyNames() {
print("\(family)\n\t\(UIFont.fontNamesForFamilyName(family))")
}
to make sure it's there, and I found
Clear Sans
["ClearSans", "ClearSans-Bold", "ClearSans-Italic"]
For some reason, it's not in here but it is in the storyboard. So I tried to use this answer, but it crashes, I assume for the same reason that I didn't find ClearSans-BoldItalic, but I don't know why it's not in there.
Finally, I tried to just "stack" the two, like so
let bold = UIFont(name: "ClearSans-Bold", size: 20)
let italic = UIFont(name: "ClearSans-Italic", size: 20)
attributedCaption.addAttribute(NSFontAttributeName, value: bold!, range: (imageContainer.caption as NSString).rangeOfString(matchString))
attributedCaption.addAttribute(NSFontAttributeName, value: italic!, range: (imageContainer.caption as NSString).rangeOfString(matchString))
but all this does is use the last attribute, which is what I had expected it to do.
Is there some other method I can use to do this so that I get both bold and italic to show on the same part of the label?
Edit
I wanted to include my fonts that are in the project, so it's clear that I've already imported them. The second image is the list of fonts on the storyboard. While it's covered up, the family is in fact Clear Sans.
Make sure bold italic version is also added to app's Info.plist under UIAppFonts ("Fonts provided by application") key.
UIFont.italicSystemFont(ofSize: 15.0)
UIFont.boldSystemFont(ofSize: 15)
if you want to custom font of label like fond system . you can try my solution .
it works good for swift 3

Formatting a letter on textView in iOS

I have two pages long letter in textView. And all context of the letter should be shown in device's screen. I'm trying to achieve this by making the font smaller. The problem is that if I copy the letter into textView, it changes the font color to all black and format of the letter becomes weird.
I tried to edit the letter format in Storyboard but it's very hard. When I try to press "Enter" in textView, it doesn't put space between the lines but rather I come out from the textView.
Is textview the best place to put long context of the letter in? If so, how should format the letter in there?
To enter space use Option+Enter and if you are copying from text editor that support formatting then please remove all formatting then copy and paste it. to format text in textview use attributed string.
Why don't you format it in text editor and save it to a rtf file then, read the rtf into an attributed string, then set the attributedText of the text view? Like:
let textURL = NSBundle.mainBundle().URLForResource("MyLovelyLetter", withExtension: "rtf")
let options =[NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType]
let attribText = try! NSAttributedString(fileURL: textURL!, options: options, documentAttributes: nil)
textView.attributedText = attribText
for example

How to fix UILabel padding issue?

I have a UILabel and whenever I set its outlet the padding I created in Storyboards for it under attributed text disappears like it was never there. The text the stays stuck on the left side. How can I fix this issue?
#IBOutlet weak var mycoollabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mycoollabel.text = "Wow"
}
Note: OP clarified in a comment that they are setting their padding using attributed text in Interface Builder.
The problem here is a confusion between text and attributed text, or, because these are code, it's text and attributedText.
On iOS, lots of things have a text property, and it's just a simple string of text that gets displayed somewhere without any sort of formatting. Usually the formatting is attached to the object that is showing the text, e.g. a label has a text colour and alignment properties that make its text look however you want.
On iOS, most (all?) of those things also have an attributedText property, which is a very different beast. Attributed text contains a string but also specific formatting instructions required to display that string – text attributes, hence the name.
So, OP is creating an attributed text configuration in their storyboard, then, in code, modifying the text property. This will overwrite their formatted string with a plain string, losing its layout configuration.
If you're looking to create an attributed string with a first line indent, you should try this:
let ps = NSMutableParagraphStyle();
ps.firstLineHeadIndent = 50
let attrs = [NSParagraphStyleAttributeName: ps]
let attributedString = NSAttributedString(string: "Hello, world!", attributes: attrs)
label.attributedText = attributedString
Note that there are a few different options available to you here, so you might need to find the right type of indent for your precise needs. If you right-click on NSMutableParagraphStyle and choose Jump To Definition you'll see all the options.
That's because a UILabel can have either plain text or attributed text. You set attributed text in the storyboard, and add some formatting (padding, etc.). Then, in code, you override that text (and all its formatting) with plain text.
If you want to keep the formatting, you need to set the label's attributedText property. And add all the necessary formatting to the NSAttributedString object you create for that.

Resources