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
Related
Hello I'm trying to create a custom text editor, for which I want to know the position of the cursor and range of the text in UITextView for which I can apply TextAttributes based on the user selected options.
For eg:
\n This is a the first paragraph string \n This is the second paragraph string \n
For this string, if the cursor is anywhere in the first paragraph string, and user select some text attribute, then I want to change only the This is a the first paragraph string based on selection.
For now, I know this can be achieved using multiple text attributes for specific range.I'm unable to get the range based on the cursor and break characters.
I'm new to this. If there is some other way to achieve this please let me know. Thanks
I'm trying to replace some text that was selected inside a UITextView with some NSAttributedString but only the following method is available:
textView.replace(UITextRange, withText: String)
As you can see, replacing text only accepts a String and I cannot find way to replace it with an NSAttributedString.
One thing I could do is to store the whole UITextView attributedText and then perform the desired changes on a NSMutableAttributedString and then I can replace the UITextView.attributedText to be the one of the NSMutableAttributedString, but this comes with some issues for me.
If the text is already long with some NSStorage and NSAttachments this will be way more expensive.
Is there any workaround?
Because you can't intermix String and NSAttributedString, there's unfortunately no workaround that will let both co-exist in a text field.
But you should be able to use replaceCharacters(in:with:):
existingAttributedString.replaceCharacters(in: range, with: replacementAttributedString)
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.
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])
I have been struggling with this. User needs to enter text and/or emoticons in a TextView. I got an emoticon keyboard with my own images to enter emoticons. Problem is I need to keep a symbol (e.g. "(smile)" for the emoticon within the text while AT THE SAME TIME showing the emoticon picture on top of the symbol.
So user would see "Hello [the picture]" while the TextView.text property would return "Hello (smile)".
On Android you can use Spanned strings which allow you to cover part of your text with an image. Thus on Android I managed to achieve my objective without problem.
On iOS, I thought Attributed Strings were a similar concept to Spanned but so far all I have been able to do is entirely replace the emoticon's code with the picture (using NSTextAttachment). Is there a way to achieve my objective without having to maintain one attributed string containing pictures and one separate string containing codes?
You can use this method, Hope it will work for you.
- (NSAttributedString*) parseEmoticons:(NSAttributedString*)text {
text = [text stringByReplacingOccurrencesOfString:#":-)" withString:#"😄"];
text = [text stringByReplacingOccurrencesOfString:#";P" withString:#"😜"];
text = [text stringByReplacingOccurrencesOfString:#"B-)" withString:#"😎"];
text = [text stringByReplacingOccurrencesOfString:#";-P" withString:#"😜"];
return text;
}
Having failed to find a more elegant solution, I resorted to maintaining one attributedstring containing the emoticon picture, and one regular string to hold the emoticon codes. So my attString is for instance "Hello [Smiling picture]" while my string is "Hello %101%". If you are interested in building a chatting app as I am, here is the pseudo code:
In emoticon keyboard:
{
Insert picture into attributed string at location loc;
Call textView shouldChangeTextInRange:(loc,0) replacementText:"%101";
}
In the view controller at shouldChangeTextInRange:(loc,length) replacementText:text:
{
Parse regular string to jump over emoticon codes already there to find the location matching loc;
Replace text (for instance %101%) in regular string} at the identified location.
}
Note: shouldChangeTextInRange is also called for regular keyboard entries including delete.