Different Text font at each line of UIlabel - ios

Is there any way to give different font size to each line of a label with "n" no of lines.
I dont want to take multiple labels for this purpose.
plz help!!!

Starting with iOS 6, you can use NSAttributedString in UILabel.
An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string.
NSMutableAttributedString *attrStr = ...
myLabel.attributedText = attrStr;

Yes, but You have to use UIWebView, pass your text in html format.

UILabel with NSAttributedString(iOS 6 and later).
Or UIWebView with html-strings, but my favorit is a UITextView with html-strings. You can use it like this:
UITextView *tView=[UITextView new];
[tView setValue:#"<b>foo</b>bar" forKey:#"contentToHTMLString"];
UITextViews are easier to handle then UIWebView, especially if you don't need all the functionality of the WebView.

If you are targeting iOS < 6.0 then by using UILabel its not at all possible. Else you can refer dasblinkenlight answer (for iOS > 6)
There are couple of methods by which you can implement same :
Use UIWebView
Create UILabel for different fonts you want and set their frame as you want to display.

Related

Accessibility : Make UILabel with Attributed text as accessibility container

I have a custom view. I capture the screenshot of view and use image in NSTextAttachment as :
UIImage *image = view.image;
NSTextAttachment *attachment = [NSTextAttachment new];
attachment.image = image;
I then create a mutable Attributed string with this attachment as:
NSAttributedString *attrStr = [NSAttributedString attributedStringWithAttachment:attachment];
This code is looped for all views and one final attributed string is created by appending all attrStr's as:
[mutableString beginEditing];
[mutableString appendAttributedString:attrStr];
[mutableString endEditing];
This is then set to UILabel attributed text.
Problem : I want voice over to iterate through individual images in attributed text. So that each image is in focus by voice over.
Thanks !!
The best way to iterate through individual images (like a collection view for instance) relies on the fact to define each element as a UIAccessibilityElement inside a wrapper whose trait property is adjustable.
To understand how that should be implemented, I suggest you take a look at the WWDC 2018 - Deliver an exceptional accessibility experience video whose content is perfectly summarized here and whose presented example can be downloaded.
Use of UIAccessibilityContainer protocol could be an interesting track of investigation as well.
Finally, I don't think you could iterate your images inside the attributed text as is but creating a kind of accessible structure perfectly understandable by VoiceOver as defined before would allow to reach your goal.

How to get the default font details used by iOS to display UITableView header title?

I am implementing tableView:viewForHeaderInSection: delegate in order to display text and icon for a grouped table view. I am using a UILabel for the text part and wish it to look exactly as titles appear when tableView:titleForHeaderInSection: is implemented.
I don't wish to use hard coded font names and font sizes in order to ensure that code will work on various iOS devices, and on various iOS versions. (especially future ones that might change these defaults).
Any ideas?
This is how I do it in another part of my app... maybe this will help you?
CGFloat preferredFontSize = [UIFont systemFontSize];
// or instead of systemFontSize; else smallSystemFontSize, labelFontSize, buttonFontSize...
UIFont *preferredFontForList = [UIFont systemFontOfSize:preferredFontSize];
PS: I think you will find that tableView:viewForHeaderInSection: will override tableView:titleForHeaderInSection: or visa versa.

UILabel how to tighten letter spacing

I would like to know how to programatically "Tighten Letter Spacing"?
This option is available for a UILabel made in a xib file, and is really handy sometimes.
I know this question has been asked before, but I don't see an answer that mentions this beeing available in the interface builder, so I was curious...
You are probably looking for this:
#property(nonatomic) BOOL adjustsLetterSpacingToFitWidth
Property of UILabel new in iOS6.
You might want to use UILabel property:
allowsDefaultTighteningForTruncation: Bool
and set it to true. (Default is false)
According to Apple Documentation:
When this property is set to true, the label tightens intercharacter spacing of its text before allowing any truncation to occur. The label determines the maximum amount of tightening automatically based on the font, current line width, line break mode, and other relevant information.
https://developer.apple.com/reference/uikit/uilabel/1620533-allowsdefaulttighteningfortrunca
adjustsLetterSpacingToFitWidth is deprecated as of iOS 7.
You'd now (as of iOS 8) probably want to do something like:
NSAttributedString *as =
[[NSAttributedString alloc] initWithString:#"Kerninating all the strings"
attributes:#{NSKernAttributeName : #(-2.0)}];
label.attributedText = as;

How to change shape of the UITextField?

I want to implement custom text fields. Here is a screenshot of what I want to implement:
So I have a UITableView with cells, and in each cell I have several text fields: one for date, one for name, one for theme, and one for text. But the UI requires theme and text to be next to each other (as can you see in the picture). I wanted to implement this as a single UITextField, but as far as I know, UITextField supports only one type of font. So maybe someone will give me a piece of advice how to implement the design shown in the screenshot — do I have to draw custom text fields, or are there simpler solutions? Any code samples or propositions would be helpful.
For iOS 6:
You can set a UITextField's attributed string:
#property(nonatomic,copy) NSAttributedString *attributedText;
I suggest to use a NSMutableAttributedString and to exploit polymorphism.
A mutable attributed string can hold vary attributes for each piece of the string. So you can add an attribute only for a certain range of the string with this method:
- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)aRange;
For iOS 5:
Use directly this UITextField's property:
- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)aRange;

How do you have two fonts in one UITextView (Xcode)?

I'm trying to make two font styes in one UITextView, how do I do this?
From the class reference:
This class does not support multiple styles for text. The font, color,
and text alignment attributes you specify always apply to the entire
contents of the text view. To display more complex styling in your
application, you need to use a UIWebView object and render your
content using HTML.
You cannot have two on the same page because it is not supported. Just use a webview and an HTML file
You can know use this by using the attributedText property of UITextView. This is available under iOS 6
UITextView supports just a single font, but there's a different topic on something similar:
Can I use multiple font style in UITextView?
If the text you want to draw is simple, I'd suggest subclassing UITextView or UIView, overwriting the drawRect function and work with some extra variables of your own. This only works if you have a very predictable system to the fonts though.
Another option is using multiple labels, which would probably need an ever more predictable setup.

Resources