I'm loading in text from a plist and displaying it within my app. Ideally I want to be able to specify more complex formatting such as italics, bold and superscript text, and display it in a custom label such as TTTAttributedLabel.
Are there any available libraries to parse a string in a given format (preferably a simple text format such as Markdown or Textile) into an NSAttributedString? I am aware that solutions are available for parsing RTF and HTML, but this is overkill for my needs - plus I'd like the text to be easily written by hand.
Edit: this is for iOS/UIKit
Caught your edit. For iOS/UIKit there is a project out there called NSAttributedString+HTML that attempts to simulate the functionality available on OS X. On OS X, you would just use some minor HTML to format the string and then parse it into NSAttributedString (or objects, or websites, or files, etc.).
The project I mentioned above attempts to offer the same extensions on iOS. I don't know why, after 6 major releases of iOS, it still lacks such rich tools and pushes all the weight on UIWebView (over WebKit) but that's how it is.
I've just added an NSString to NSAttributedString lightweight markup parser to MGBoxKit. It's not Markdown or Textile but it's very similar. So far it supports bold, italics, underline, monospacing, and coloured text.
The MGMushParser class could be used standalone, and is fairly easy to extend.
NSString *markup = #"**bold**, //italics//, __underlining__, and `monospacing`, and {#0000FF|coloured text}";
UIFont *baseFont = [UIFont fontWithName:#"HelveticaNeue" size:18];
UIColor *textColor = UIColor.whiteColor;
myLabel.attributedString = [MGMushParser attributedStringFromMush:markup
font:baseFont color:textColor];
OHAttributedLabel also has a similar markup parser.
Related
Hi I am using below code to use custom font style for label text. I've installed it and work fine but the problem is that the size of text does not change. I've tried more times with different sizes. I don't know what should do
self.saveButton_label.text = NSLocalizedString(#"Saved", nil);
self.saveButton_label.font = [UIFont fontWithName:#"Helvetica Neue LT Pro" size: 13.0];
I believe the issue lies with one of the steps you need to take when adding a custom font to your project, you probably are not noticing the difference because Helvetica Neue is also the default font, which I believe is used if you were to assign nil to UILabel as the font.
I suggest following this guide, I've used it as a checklist myself when using custom fonts and it has worked, especially take not of the naming, the name you have to use in code might not be the one you see in FontBook or when using Preview on the font.
Try this code, In your code you are missing "-" with font name.
self.saveButton_label.text = #"Saved";
[self.saveButton_label setFont:[UIFont fontWithName:#"Helvetica-Neue-LT-Pro size:13.0f]];
also you can use below fonts.
HelveticaNeueLTPro-MdCn
HelveticaNeueLTPro-BdCn
Hope it will work for you.
Consider the following, I have paragraph data being sent to a view which needs to be placed over a background image, which has at the top and the bottom, fixed elements (fig1)
Fig1.
My thought was to split this into 4 labels (Fig1.example2) my question here is how I can get the text to flow through labels 1 - 4 given that label 1,2 & 3 ar of fixed height. I assumed here that label 3 should be populated prior to 4 hence the layout in the attached diagram.
Can someone suggest the best way of doing this with maybe an example?
Thanks
Wish I could help more, but I think I can at least point you in the right direction.
First, your idea seems very possible, but would involve lots of calculations of text size that would be ugly and might not produce ideal results. The way I see it working is a binary search of testing portions of your string with sizeWithFont: until you can get the best guess for what the label will fit into that size and still look "right". Then you have to actually break up the string and track it in pieces... just seems wrong.
In iOS 6 (unfortunately doesn't apply to you right now but I'll post it as a potential benefit to others), you could probably use one UILabel and an NSAttributed string. There would be a couple of options to go with here, (I haven't done it so I'm not sure which would be the best) but it seems that if you could format the page with html, you can initialize the attributed string that way.
From the docs:
You can create an attributed string from HTML data using the initialization methods initWithHTML:documentAttributes: and initWithHTML:baseURL:documentAttributes:. The methods return text attributes defined by the HTML as the attributes of the string. They return document-level attributes defined by the HTML, such as paper and margin sizes, by reference to an NSDictionary object, as described in “RTF Files and Attributed Strings.” The methods translate HTML as well as possible into structures of the Cocoa text system, but the Application Kit does not provide complete, true rendering of arbitrary HTML.
An alternative here would be to just use the available attributes, setting line indents and such according to the image size. I haven't worked with attributed strings at this level, so I the best reference would be the developer videos and the programming guide for NSAttributedString. https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/AttributedStrings/AttributedStrings.html#//apple_ref/doc/uid/10000036-BBCCGDBG
For lesser versions of iOS, you'd probably be better off becoming familiar with CoreText. In the end you'll be rewarded with a better looking result, reusability/flexibility, the list goes on. For that, I would start with the CoreText programming guide: https://developer.apple.com/library/mac/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html
Maybe someone else can provide some sample code, but I think just looking through the docs will give you less of a headache than trying to calculate 4 labels like that.
EDIT:
I changed the link for CoreText
You have to go with CoreText: create your AttributedString and a CTFramesetter with it.
Then you can get a CTFrame for each of your textboxes and draw it in your graphics context.
https://developer.apple.com/library/mac/#documentation/Carbon/Reference/CTFramesetterRef/Reference/reference.html#//apple_ref/doc/uid/TP40005105
You can also use a UIWebView
I need to take an existing pdf file and programmatically fill in a list of form fields with text and then save the pdf without ever displaying it to the user.
For instance, if the pdf file contains fields named "LastName", and "FirstName" I would like to set the value of "FirstName" to "Louis" and then save the file.
I've been searching for a long time and can't find any guidance on even where to start since the iOS documentation (and most of the questions on here) seem geared towards displaying or creating pdf content instead of modifying it.
EDIT:
My main question is: Is it possible to open a pdf stream (I know how to do this) and copy each existing pdf dictionary item into a new pdf? I have not been able to find a way to write the dictionary items to a pdf.
I doubt that kind of functionality will ever be in the iOS frameworks. The reason most of the related info you can find "seem[s] geared towards displaying or creating pdf content instead of modifying it" is because that's what the vast majority of use cases will want or need for PDF functionality.
You'll need to find a 3rd party library that can open up PDFs, fill out the AcroForm fields, and then stamp out a PDF. I use iText on Java (there is also iTextSharp for C#) but I don't know of anything for Objective-C.
Once you find that library, you'll need to integrate it into your project. There are undoubtedly several related questions/answers here on SO for whatever version of the SDK you're using.
This would be easier to do with a HTML page. If you wish to use a HTML page instead of a .pdf form then thius is how you would go about doing it:
[field1 setText:#"Field 1 Text"];
[field2 setText:#"Field 2 Text"];
NSString *result;
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"$('#field1').val('%#');", field1.text]];
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"$('#pfield2').val('%#');", field2.text]];
result = [webView stringByEvaluatingJavaScriptFromString:#"$('#submitbutton').click();"];
You would need to create two UILabels or UITextFields and call them "field2" and "field2" in your .h file. You can then find the ID of the field you need to replace e.g. #field1 and then put it where I have put "#field1" and again for the second field where I have put "#field2". There also needs to be a UIWebView with the page already loaded. This code is to be used after the UIWebView page has been loaded. Maybe do the following:
-(void)webViewDidFinishLoad:(UIWebView *)webView {
// Insert above code here
}
You probably need a full understanding of Javascript if you want to do this for the whole form, but this should get you started.
Hope that helps you.
I used a special font in my photoshop design,
is it possible to use these fonts natively in the iphone app? or
do I need to convert them into an image first?
If this is the only way to do it, what do you do with dynamic text?
Copy your font file into Resources
In your application .plist create (if it's exist just create a row) a row called "Fonts provided by application" and then in "item 0" copy your font name for example "Ciutadella-Bold.otf" (if there are some spaces in font name, rename it and for example replace all spaces to minus '-')
Then you can define this font in your application:
UIFont *CiutadellaBold = [UIFont fontWithName:#"Ciutadella-Bold" size:17.0f];
And use in for instance in uiLabel:
[uiLabel setFont:CiutadellaBold];
UIFont *font = [UIFont fontWithName:#"MyFont" size:20];
[label setFont:font];
Where "MyFont" would be a TrueType or OpenType file in your project (sans the file extension), and label would be an instance of UILabel.
Yes it is possible with iOS 3.2 and later. You need to have the fonts file, I forget what formats exactly. Add the font file as a resource to your project then add the names of the fonts in the applications info.plist in an array under the raw key 'UIAppFonts' or under the friendly key 'Fonts provided by application'.
Here's a sample application that you can look at - https://files.me.com/tobiasoleary/ey08n1. It prints out the all the fonts accessible to the application. Two fonts have been added Flames and Firestarter.
This is by far the easiest way to add custom fonts to application. If you need to support custom fonts for iOS before 4.0 see http://github.com/zynga/FontLabel/tree/master
This question has been asked before here: Can I embed a custom font in an iPhone application?.
Note :
The parameter of “fontWithName” must be the real name of the font, not the name of the file. If you open your ttf file with the Mac Font Book, you will directly see its name on top of the window.
I need help in using these symbols ⎕, ∨, ๐, Ʌ, and so on. But when I create a PDF with iText these symbols do not appear.
What can I do so that these symbols appear?
You have to use a font and encoding that contains those characters. Your best bet is to use IDENTITY_H for your encoding, as this grants you access to every character within a given font... but you still have to use the right font.
There are several font-manipulation examples within "iText in Action's" chapter on fonts:
http://www.itextpdf.com/book/chapter.php?id=11
The examples are down the right side. Buying the book would probably help too.
I had the same problem too and I figured out using IDENTITY_H for encoding is working fine.
For example:
java.awt.Font f =...;
Font font = FontFactory.getFont(f.getName(),BaseFont.IDENTITY_H)
I don't understand why with BaseFont.WINANSI it doesn't work. Winansi is the standard Windows Cp1252 character set, that one used by my JVM. So, if the char is correctly displayed in Java, why it is not the case for PDF?
You can escape them according to the unicode escape sequence defined in the java language specification. See http://java.sun.com/docs/books/jls/first_edition/html/3.doc.html
If you are using IntelliJ IDEA for your code you can download the StringManipulation plugin, that does the escapes for you. In the settings of IDEA you can also set the "Transparent native-to-ascii conversion" checkbox under File encodings, and this should help do the trick.
square in pdf file by iText:
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arialbd.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
question.add(new Phrase("\u25A1", new Font(bf, 26)));
You can see a pdf file exemple here