Hello am using NSMutableAttributedString to show different fonts but with same size in UIAlertController, the problem here is it showing two different fonts which is correct but it also showing two different sizes.Here is my code
UIFont *regularFont = [UIFont fontWithName:#"HelveticaNeue" size:11.0];
UIFont *boldFont = [UIFont fontWithName:#"HelveticaNeue-Bold" size:11.0];
UIAlertController *alertObj = [UIAlertController alertControllerWithTitle:nil message:messageStr preferredStyle:UIAlertControllerStyleAlert];
NSMutableAttributedString *attMessage = [[NSMutableAttributedString alloc] initWithString:messageStr];
[attMessage addAttribute:NSFontAttributeName value:regularFont range:NSRangeFromString(messageStr)];
[attMessage addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(25, 6)];
[attMessage addAttribute:NSFontAttributeName value:boldFont range:NSMakeRange(35, 15)];
[alertObj setValue:attMessage forKey:#"attributedMessage"];
Result can be match.Any help would be appreciated
The attributedMessage property is not public, using it is a good way to get your app rejected at the review. If you really need to style the dialogue and distribute the app on the App Store, I think you should build your own component to display the pop-up.
Related
I am having trouble using a custom font in my application.
The following describes the steps I took
1 - Add .TTF font to application
2 - Modify the info.plist file.
3 - Add the key "Fonts provided by application" to a new row
4 - And add each .TTF file (of font) to each line.
So I have an array of fonts, each of which has a .ttf extension.
And then I try to add the font
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(25, 65, 230, 33)];
lblName.backgroundColor = [UIColor clearColor];
[lblName setFont: [UIFont fontWithName:#"acmesab" size:32]]; // acmesab is my custom "font" name.
lblName.textAlignment = NSTextAlignmentLeft;
lblName.text = #"Name:";
[simapleView addSubview:lblName];
Open your project -> Targeta -> Build Phase -> Copy Bundle Resources. Check your files are shows in list or not? If not then add them and run the application again.It should work.
Use following code to check whether font is added or not and add appropriate font name from listing.
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
NSLog(#"Family name: %#", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSLog(#" Font name: %#", [fontNames objectAtIndex:indFont]);
}
[fontNames release];
}
[familyNames release];
For the custom font first you have to install your .ttf file in your system.after that import it to your project. Rename .ttf file as shown in "FontBook" and
add
<key>UIAppFonts</key>
<array>
<string>Times New Roman.ttf</string>
</array>
in your info.plist file. and then use it as fontname in your project
CCLabelTTF *label1 = [CCLabelTTF labelWithString:#"Name" fontName:#"Times New Roman" fontSize:26];
You should look at your font-properties what the actual font-name is.
Sometimes you import the font with name FjallaOne-Regular. But if you look at the properties you see that the font is named like Fjalla One.
Therefore you need to use it like this.
[lblName setFont: [UIFont fontWithName:#"Fjalla One" size:32]];
Instead of
[lblName setFont: [UIFont fontWithName:#"FjallaOne-Regular" size:32]];
Also check if you set the correct target membership
You can see what I mean by the correct name over here
LINK
It can be tricky sometimes as certain fonts don't seem to be recognised. Have a look at my answer from a few months ago which might help you - https://stackoverflow.com/a/10170599/331854
How to change the color of the UILabel text gradually like the following link?
Can any one suggest me some code?
You can use formatted text.
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:#"Hello World"];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(1,5)];
iOS < 6
Second you need to subclass UILabel and print this string inside the drawRect method. You need to create a some type of loop that changes the color according to the speech speed.
iOS 6
You can use the attributedTextproperty (no need to subclass)
(void)drawTextInRect:(CGRect)rect
or reuse code:
https://github.com/mattt/TTTAttributedLabel
The app that you linked [http://www.youtube.com/watch?v=_vOYvaNhSHw] , probably is maded using cocos2d.
In cocos2d, you can change text color easily also with animation.
Here an example:
http://www.cocos2d-iphone.org/forum/topic/5903
Here cosos2d sdk, i suggest to try, because it's very powerful:
http://www.cocos2d-iphone.org/
enjoy.
here is one of my sample code. using block method of TTTAttributedLabel class it may help you .
[cell.lblAtt setText:strAtt afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
UIFont *italicSystemFont = [UIFont boldSystemFontOfSize:12];
CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
NSUInteger lenght = [[tempObj objectForKey:#"username"] length];
NSUInteger lenght2 = [[NSString stringWithFormat:#"%d",[tempArr count]] length];
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[ThemeColor CGColor] range:NSMakeRange(0,lenght)];
[mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName value:(__bridge UIFont*)italicFont range:NSMakeRange(0,lenght)];
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[ThemeColor CGColor] range:NSMakeRange(lenght+11,lenght2)];
[mutableAttributedString addAttribute:(NSString*)kCTFontAttributeName value:(__bridge UIFont*)italicFont range:NSMakeRange(lenght+11,lenght2)];
return mutableAttributedString;
}];
Use NSAtributedString in UILabel from iOS 6.0. For lesser version below iOS 6.0 use TTTAttributedLabel which supports NSAtributedString
Change attributed string according to your requirement by setting it again in UILabel
EDIT add colored text as u want for example in loop
For 1st second in Label : I am good boy.
For 2nd second in Label : I am good boy.
For 3rd second in Label : I am good boy.
For 4th second in Label : I am good boy.
For 5th second in Label : I am good boy.
I have an app where the users enters information in a few textfields which are then added to a uitextview by a nsstring does anybody know how i can set one of the textfields to italics and keep the others normal
regards
Edit:
Heres my code i want to only change once textfield (e.g textfbookpublisher):
NSString* combinedString = [NSString stringWithFormat:
#"%#,%#.'%#.'%#.%#,%#.",
textfbookpublisher.text,
textfbookauthor.text,
textfbooktitle.text,
textfbookplace.text,
nameofuni.text,
textfbookyear.text];
DissertationGen*bookg = [[DissertationGen alloc] init];
bookg.message = combinedString;
bookg.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:bookg animated:YES]
A quick search would have given you an easy answer. For example, this post.
Simply set the font of the UITextfield you want to a [UIFont italicSystemFontOfSize:].
try this link see accepted answer and also check TTTAttributedLabel library
Bold one Word
now you want only textfbookpublisher in italic then pass it individually to next DissertationGen view
in DissertationGen controller's viewdidload method use following code
UILabel *lblpubl = [[UILabel alloc] init];
lblpubl.font = [UIFont italicSystemFontOfSize:18.0];
lblpubl.text = your variable which contains textfbookpublisher's text;
now use lblpubl as you wish
I'm using setTitleTextAttributes to change the font on a UISegmentedControl as follows.
UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
[self setTitleTextAttributes:attributes forState:UIControlStateNormal];</pre></code>
This works fine in the simulator, but crashed when running on an iPad. The bad access fault occurs on creation of the dictionary and I used an NSLog statement to verify that UITextAttributeFont is generating the fault.
Any ideas what the problem is here or if there is an alternative method to set the font?
EDITED
I just realized the iPad is running iOS 4 (I believe this is supported on iOS >= 5). What is the best way to test for version support?
Based on Dirty Henry's suggestion, the correct implementation is as follows.
if ([self respondsToSelector:#selector(setTitleTextAttributes: forState:)]) {
UIFont *font = [UIFont boldSystemFontOfSize:14.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: font, UITextAttributeFont, nil];
[self setTitleTextAttributes:attributes forState:UIControlStateNormal];
}
Is your iPad running iOS 5.0 or later? (UITextAttributeFont has been introduced in this version). (i don't see anything wrong with your code either)
I want to change the font of label. And font which i am using is shown in image.
How use it in my application. I have already add in .plist as show in image. But it not working proper. How i manage it?
Thanks in advances...
Use this to set the font programmatically:
[TheLabelName setFont:[UIFont fontWithName:#"American Typewriter" size:18]];
Custom fonts in IOS
to set an label font just
yourLabel.font = [UIFont fontWithName:#"CloisterBlack" size:64.0];
If you want to use the standard font, then just as usually: you can set it in the interface builder for this label or programmatically for this label.
If you want to apply your custom font (according to the image you want that), then you can do smth. like
UIFont *font = [UIFont fontWithName:#"MyFont" size:20];
[label setFont:font];
Also you can explore this ref:
Can I embed a custom font in an iPhone application?
it might be useful in your case.
Add your custom font file .ttf in resourse and use every time when you want to display formatted font like
headLbl.font = [UIFont fontWithName:#"Museo-700" size:20.f];
Add a font in your resource directory like this image that you have already done now main thing you cann't wirte the name of the font that you have added as it is check my image font having name ROCKB.ttf but i have write the Rockwell-Bold so you have to check by what name it has been installed in your code then pick the name from there and then put that name in lable the font will reflect the label now.
label.font = [UIFont fontWithName:#"Rockwell-Bold" size:20];
and font family by this code NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
NSLog(#"Family name: %#", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont)
{
NSLog(#" Font name: %#", [fontNames objectAtIndex:indFont]);
}
[fontNames release];
}
[familyNames release];